{"id":616,"date":"2023-02-26T10:58:31","date_gmt":"2023-02-26T02:58:31","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=616"},"modified":"2023-04-29T20:18:55","modified_gmt":"2023-04-29T12:18:55","slug":"opencart-twig-template-engine-add-evaluate-filter","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/opencart-twig-template-engine-add-evaluate-filter\/","title":{"rendered":"OpenCart Twig\u6a21\u677f\u5f15\u64ce\u6dfb\u52a0evaluate\u8fc7\u6ee4\u5668"},"content":{"rendered":"<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/stackoverflow.com\/questions\/10945200\/twig-variables-in-twig-variable\">https:\/\/stackoverflow.com\/questions\/10945200\/twig-variables-in-twig-variable<\/a><\/p>\n<h2>Twig\u8fc7\u6ee4\u5668<\/h2>\n<p>Twig Extension:<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">&lt;?php\n\n\/**\n* A twig extension that will add an &quot;evaluate&quot; filter, for dynamic evaluation.\n*\/\nclass EvaluateExtension extends \\Twig_Extension {\n    \/**\n    * Attaches the innervars filter to the Twig Environment.\n    * \n    * @return array\n    *\/\n    public function getFilters( ) {\n        return array(\n            &#039;evaluate&#039; =&gt; new \\Twig_Filter_Method( $this, &#039;evaluate&#039;, array(\n                &#039;needs_environment&#039; =&gt; true,\n                &#039;needs_context&#039; =&gt; true,\n                &#039;is_safe&#039; =&gt; array(\n                    &#039;evaluate&#039; =&gt; true\n                )\n            ))\n        );\n    }\n\n    \/**\n     * This function will evaluate $string through the $environment, and return its results.\n     * \n     * @param array $context\n     * @param string $string \n     *\/\n    public function evaluate( \\Twig_Environment $environment, $context, $string ) {\n        $loader = $environment-&gt;getLoader( );\n\n        $parsed = $this-&gt;parseString( $environment, $context, $string );\n\n        $environment-&gt;setLoader( $loader );\n        return $parsed;\n    }\n\n    \/**\n     * Sets the parser for the environment to Twig_Loader_String, and parsed the string $string.\n     * \n     * @param \\Twig_Environment $environment\n     * @param array $context\n     * @param string $string\n     * @return string \n     *\/\n    protected function parseString( \\Twig_Environment $environment, $context, $string ) {\n        $environment-&gt;setLoader( new \\Twig_Loader_String( ) );\n        return $environment-&gt;render( $string, $context );\n    }\n\n    \/**\n     * Returns the name of this extension.\n     * \n     * @return string\n     *\/\n    public function getName( ) {\n        return &#039;evaluate&#039;;\n    }\n}<\/code><\/pre>\n<p>Example usage:<\/p>\n<pre><code class=\"language-php\">$twig_environment-&gt;addExtension( new EvaluateExtension( ) );<\/code><\/pre>\n<p>In the template:<\/p>\n<pre><code class=\"language-html\">{% set var = &#039;inner variable&#039; %}\n{{&#039;this is a string with an {{var}}&#039;|evaluate}}<\/code><\/pre>\n<h2>OpenCart Twig Evaluate<\/h2>\n<p>system\/library\/template\/TwigEvaluateExtension.php<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\/**\n * Created by PhpStorm.\n * User: yezhou\n * Date: 2018\/5\/23\n * Time: 18:26\n *\/\n\n\/**\n * A twig extension that will add an &quot;evaluate&quot; filter, for dynamic evaluation.\n *\/\nclass TwigEvaluateExtension extends \\Twig_Extension\n{\n    \/**\n     * Attaches the innervars filter to the Twig Environment.\n     *\n     * @return array\n     *\/\n    public function getFilters()\n    {\n        return array(\n            &#039;evaluate&#039; =&gt; new \\Twig_Filter_Method($this, &#039;evaluate&#039;, array(\n                &#039;needs_environment&#039; =&gt; true,\n                &#039;needs_context&#039; =&gt; true,\n                &#039;is_safe&#039; =&gt; array(\n                    &#039;evaluate&#039; =&gt; true\n                )\n            ))\n        );\n    }\n\n    \/**\n     * This function will evaluate $string through the $environment, and return its results.\n     *\n     * @param array $context\n     * @param string $string\n     *\/\n    public function evaluate(\\Twig_Environment $environment, $context, $string)\n    {\n        $loader = $environment-&gt;getLoader();\n\n        $parsed = $this-&gt;parseString($environment, $context, $string);\n\n        $environment-&gt;setLoader($loader);\n        return $parsed;\n    }\n\n    \/**\n     * Sets the parser for the environment to Twig_Loader_String, and parsed the string $string.\n     *\n     * @param \\Twig_Environment $environment\n     * @param array $context\n     * @param string $string\n     * @return string\n     *\/\n    protected function parseString(\\Twig_Environment $environment, $context, $string)\n    {\n        $environment-&gt;setLoader(new \\Twig_Loader_String());\n        return $environment-&gt;render($string, $context);\n    }\n\n    \/**\n     * Returns the name of this extension.\n     *\n     * @return string\n     *\/\n    public function getName()\n    {\n        return &#039;evaluate&#039;;\n    }\n}<\/code><\/pre>\n<p>system\/library\/template\/twig.php<\/p>\n<pre><code class=\"language-php\">$this-&gt;twig = new \\Twig_Environment($loader, $config);\n$this-&gt;twig-&gt;addExtension(new \\TwigEvaluateExtension());<\/code><\/pre>\n<p>system\/storage\/modification\/system\/library\/template\/twig.php<\/p>\n<pre><code class=\"language-php\">$this-&gt;twig = new \\Twig_Environment($loader, $config);\n$this-&gt;twig-&gt;addExtension(new \\TwigEvaluateExtension());<\/code><\/pre>\n<h2>Twig Evaluate \u5b9e\u4f8b<\/h2>\n<h3>\u5b9e\u4f8b1<\/h3>\n<ul>\n<li>tpl<\/li>\n<\/ul>\n<pre><code class=\"language-html\">&lt;div class=&quot;col-sm-10&quot;&gt;\n  &lt;select name=&quot;&lt;?php echo $pm; ?&gt;_status&quot; id=&quot;input-status&quot; class=&quot;form-control&quot;&gt;\n    &lt;?php $pm_status = $pm . &#039;_status&#039;; \n    if ($$pm_status) { ?&gt;\n    &lt;option value=&quot;1&quot; selected=&quot;selected&quot;&gt;&lt;?php echo $text_enabled; ?&gt;&lt;\/option&gt;\n    &lt;option value=&quot;0&quot;&gt;&lt;?php echo $text_disabled; ?&gt;&lt;\/option&gt;\n    &lt;?php } else { ?&gt;\n    &lt;option value=&quot;1&quot;&gt;&lt;?php echo $text_enabled; ?&gt;&lt;\/option&gt;\n    &lt;option value=&quot;0&quot; selected=&quot;selected&quot;&gt;&lt;?php echo $text_disabled; ?&gt;&lt;\/option&gt;\n    &lt;?php } ?&gt;\n  &lt;\/select&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<ul>\n<li>twig<\/li>\n<\/ul>\n<pre><code class=\"language-html\">&lt;div class=&quot;col-sm-10&quot;&gt;\n  &lt;select name=&quot;payment_{{ pm }}_status&quot; id=&quot;input-status&quot; class=&quot;form-control&quot;&gt;\n    {% set pm_status = pm ~ &#039;_status&#039; %}\n    {% set pm_status_s = &#039;{{ &#039; ~ pm_status ~ &#039; }}&#039; %}\n    {% if pm_status_s |evaluate %}\n    &lt;option value=&quot;1&quot; selected=&quot;selected&quot;&gt;{{ text_enabled }}&lt;\/option&gt;\n    &lt;option value=&quot;0&quot;&gt;{{ text_disabled }}&lt;\/option&gt;\n    {% else %}\n    &lt;option value=&quot;1&quot;&gt;{{ text_enabled }}&lt;\/option&gt;\n    &lt;option value=&quot;0&quot; selected=&quot;selected&quot;&gt;{{ text_disabled }}&lt;\/option&gt;\n    {% endif %}\n  &lt;\/select&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<h3>\u5b9e\u4f8b2<\/h3>\n<ul>\n<li>tpl<\/li>\n<\/ul>\n<pre><code class=\"language-html\">&lt;div class=&quot;col-sm-10&quot;&gt;\n  &lt;input type=&quot;text&quot; name=&quot;&lt;?php echo $pm; ?&gt;_sort_order&quot; value=&quot;&lt;?php $pm_sortorder = $pm . &#039;_sort_order&#039;;echo $$pm_sortorder; ?&gt;&quot; placeholder=&quot;&lt;?php echo $entry_sort_order; ?&gt;&quot; id=&quot;input-sort-order&quot; class=&quot;form-control&quot; \/&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<ul>\n<li>twig<\/li>\n<\/ul>\n<pre><code class=\"language-html\">&lt;div class=&quot;col-sm-10&quot;&gt;\n  {% set pm_sortorder = pm ~ &#039;_sort_order&#039; %}\n  {% set channel_sortorder = &#039;{{ &#039; ~ pm_sortorder ~ &#039; }}&#039; %}\n  &lt;input type=&quot;text&quot; name=&quot;payment_{{ pm }}_sort_order&quot; value=&quot;{{ channel_sortorder |evaluate }}&quot; placeholder=&quot;{{ entry_sort_order }}&quot; id=&quot;input-sort-order&quot; class=&quot;form-control&quot; \/&gt;\n&lt;\/div&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\uff1ahttps:\/\/stackoverflow.com\/questions\/10945200\/twig-va [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[173],"tags":[174],"class_list":["post-616","post","type-post","status-publish","format-standard","hentry","category-opencart","tag-twig"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}