1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Recipe plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* A comprehensive recipe FieldType for Craft CMS that includes metric/imperial |
6
|
|
|
* conversion, portion calculation, and JSON-LD microdata support |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2020 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\recipe\helpers; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\helpers\Template; |
16
|
|
|
use craft\web\View; |
17
|
|
|
use Twig\Markup; |
18
|
|
|
use yii\base\Exception; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package Recipe |
|
|
|
|
23
|
|
|
* @since 1.1.0 |
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class PluginTemplate |
26
|
|
|
{ |
27
|
|
|
// Static Methods |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
public static function renderStringTemplate(string $templateString, array $params = []): string |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
try { |
33
|
|
|
$html = Craft::$app->getView()->renderString($templateString, $params); |
34
|
|
|
} catch (\Exception $exception) { |
35
|
|
|
$html = Craft::t( |
36
|
|
|
'recipe', |
37
|
|
|
'Error rendering template string -> {error}', |
38
|
|
|
['error' => $exception->getMessage()] |
39
|
|
|
); |
40
|
|
|
Craft::error($html, __METHOD__); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $html; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Render a plugin template |
48
|
|
|
* |
49
|
|
|
* @param string $templatePath |
|
|
|
|
50
|
|
|
* @param array $params |
|
|
|
|
51
|
|
|
* @return Markup |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
public static function renderPluginTemplate(string $templatePath, array $params = []): Markup |
54
|
|
|
{ |
55
|
|
|
$exception = null; |
56
|
|
|
$htmlText = ''; |
|
|
|
|
57
|
|
|
// Stash the old template mode, and set it Control Panel template mode |
58
|
|
|
$oldMode = Craft::$app->view->getTemplateMode(); |
59
|
|
|
// Look for a frontend template to render first |
60
|
|
|
try { |
61
|
|
|
Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
62
|
|
|
} catch (Exception $exception) { |
63
|
|
|
Craft::error($exception->getMessage(), __METHOD__); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Render the template with our vars passed in |
67
|
|
|
try { |
68
|
|
|
$htmlText = Craft::$app->view->renderTemplate('recipe/' . $templatePath, $params); |
69
|
|
|
$templateRendered = true; |
70
|
|
|
} catch (\Exception $exception) { |
71
|
|
|
$templateRendered = false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// If no frontend template was found, try our built-in template |
75
|
|
|
if (!$templateRendered) { |
76
|
|
|
try { |
77
|
|
|
Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
78
|
|
|
} catch (Exception $exception) { |
79
|
|
|
Craft::error($exception->getMessage(), __METHOD__); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Render the template with our vars passed in |
83
|
|
|
try { |
84
|
|
|
$htmlText = Craft::$app->view->renderTemplate('recipe/' . $templatePath, $params); |
85
|
|
|
$templateRendered = true; |
86
|
|
|
} catch (\Exception $exception) { |
87
|
|
|
$templateRendered = false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Only if the template didn't render at all should we log an error |
92
|
|
|
if (!$templateRendered) { |
93
|
|
|
$htmlText = Craft::t( |
94
|
|
|
'recipe', |
95
|
|
|
'Error rendering `{template}` -> {error}', |
96
|
|
|
['template' => $templatePath, 'error' => $exception->getMessage()] |
|
|
|
|
97
|
|
|
); |
98
|
|
|
Craft::error($htmlText, __METHOD__); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Restore the old template mode |
102
|
|
|
try { |
103
|
|
|
Craft::$app->view->setTemplateMode($oldMode); |
104
|
|
|
} catch (Exception $exception) { |
105
|
|
|
Craft::error($exception->getMessage(), __METHOD__); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return Template::raw($htmlText); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|