Passed
Push — v3 ( 1e51d3...178405 )
by Andrew
25:46
created

src/helpers/PluginTemplate.php (1 issue)

1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\helpers;
13
14
use nystudio107\seomatic\Seomatic;
15
16
use nystudio107\minify\Minify;
17
18
use Craft;
19
use craft\helpers\Template;
20
use craft\web\View;
21
22
use yii\base\Exception;
23
24
/**
25
 * @author    nystudio107
26
 * @package   Seomatic
27
 * @since     3.0.0
28
 */
29
class PluginTemplate
30
{
31
    // Constants
32
    // =========================================================================
33
34
    const MINIFY_PLUGIN_HANDLE = 'minify';
35
36
    // Static Methods
37
    // =========================================================================
38
39
    public static function renderStringTemplate(string $templateString, array $params = []): string
0 ignored issues
show
You must use "/**" style comments for a function comment
Loading history...
40
    {
41
        try {
42
            $html = Seomatic::$view->renderString($templateString, $params);
43
        } catch (\Exception $e) {
44
            $html = Craft::t(
45
                'seomatic',
46
                'Error rendering template string -> {error}',
47
                ['error' => $e->getMessage()]
48
            );
49
            Craft::error($html, __METHOD__);
50
        }
51
52
        return $html;
53
    }
54
55
    /**
56
     * Render a plugin template
57
     *
58
     * @param string      $templatePath
59
     * @param array       $params
60
     * @param string|null $minifier
61
     *
62
     * @return string
63
     */
64
    public static function renderPluginTemplate(
65
        string $templatePath,
66
        array $params = [],
67
        string $minifier = null
68
    ): string {
69
        $template = 'seomatic/' . $templatePath;
70
        $oldMode = Craft::$app->view->getTemplateMode();
71
        // Look for the template on the frontend first
72
        try {
73
            $templateMode = View::TEMPLATE_MODE_CP;
74
            if (Craft::$app->view->doesTemplateExist($template, View::TEMPLATE_MODE_SITE)) {
75
                $templateMode = View::TEMPLATE_MODE_SITE;
76
            }
77
            Craft::$app->view->setTemplateMode($templateMode);
78
        } catch (Exception $e) {
79
            Craft::error($e->getMessage(), __METHOD__);
80
        }
81
82
        // Render the template with our vars passed in
83
        try {
84
            $htmlText = Craft::$app->view->renderTemplate($template, $params);
85
            if ($minifier) {
86
                // If Minify is installed, use it to minify the template
87
                $minify = Craft::$app->getPlugins()->getPlugin(self::MINIFY_PLUGIN_HANDLE);
88
                if ($minify) {
89
                    $htmlText = Minify::$plugin->minify->$minifier($htmlText);
90
                }
91
92
            }
93
        } catch (\Exception $e) {
94
            $htmlText = Craft::t(
95
                'seomatic',
96
                'Error rendering `{template}` -> {error}',
97
                ['template' => $templatePath, 'error' => $e->getMessage()]
98
            );
99
            Craft::error($htmlText, __METHOD__);
100
        }
101
102
        // Restore the old template mode
103
        try {
104
            Craft::$app->view->setTemplateMode($oldMode);
105
        } catch (Exception $e) {
106
            Craft::error($e->getMessage(), __METHOD__);
107
        }
108
109
        return Template::raw($htmlText);
110
    }
111
}
112