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 |
||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
9 | * @copyright Copyright (c) 2020 nystudio107 |
||||
0 ignored issues
–
show
|
|||||
10 | */ |
||||
0 ignored issues
–
show
|
|||||
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 | /** |
||||
0 ignored issues
–
show
|
|||||
21 | * @author nystudio107 |
||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||
22 | * @package Recipe |
||||
0 ignored issues
–
show
|
|||||
23 | * @since 1.1.0 |
||||
0 ignored issues
–
show
|
|||||
24 | */ |
||||
0 ignored issues
–
show
|
|||||
25 | class PluginTemplate |
||||
26 | { |
||||
27 | // Static Methods |
||||
28 | // ========================================================================= |
||||
29 | |||||
30 | public static function renderStringTemplate(string $templateString, array $params = []): string |
||||
0 ignored issues
–
show
|
|||||
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 |
||||
0 ignored issues
–
show
|
|||||
50 | * @param array $params |
||||
0 ignored issues
–
show
|
|||||
51 | * @return Markup |
||||
0 ignored issues
–
show
|
|||||
52 | */ |
||||
53 | public static function renderPluginTemplate(string $templatePath, array $params = []): Markup |
||||
54 | { |
||||
55 | $exception = null; |
||||
56 | $htmlText = ''; |
||||
0 ignored issues
–
show
|
|||||
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()] |
||||
0 ignored issues
–
show
The method
getMessage() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
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 |