OpenCart Twig模板引擎添加evaluate过滤器

参考:https://stackoverflow.com/questions/10945200/twig-variables-in-twig-variable

Twig过滤器

Twig Extension:

<?php

/**
* A twig extension that will add an "evaluate" filter, for dynamic evaluation.
*/
class EvaluateExtension extends \Twig_Extension {
    /**
    * Attaches the innervars filter to the Twig Environment.
    * 
    * @return array
    */
    public function getFilters( ) {
        return array(
            'evaluate' => new \Twig_Filter_Method( $this, 'evaluate', array(
                'needs_environment' => true,
                'needs_context' => true,
                'is_safe' => array(
                    'evaluate' => true
                )
            ))
        );
    }

    /**
     * This function will evaluate $string through the $environment, and return its results.
     * 
     * @param array $context
     * @param string $string 
     */
    public function evaluate( \Twig_Environment $environment, $context, $string ) {
        $loader = $environment->getLoader( );

        $parsed = $this->parseString( $environment, $context, $string );

        $environment->setLoader( $loader );
        return $parsed;
    }

    /**
     * Sets the parser for the environment to Twig_Loader_String, and parsed the string $string.
     * 
     * @param \Twig_Environment $environment
     * @param array $context
     * @param string $string
     * @return string 
     */
    protected function parseString( \Twig_Environment $environment, $context, $string ) {
        $environment->setLoader( new \Twig_Loader_String( ) );
        return $environment->render( $string, $context );
    }

    /**
     * Returns the name of this extension.
     * 
     * @return string
     */
    public function getName( ) {
        return 'evaluate';
    }
}

Example usage:

$twig_environment->addExtension( new EvaluateExtension( ) );

In the template:

{% set var = 'inner variable' %}
{{'this is a string with an {{var}}'|evaluate}}

OpenCart Twig Evaluate

system/library/template/TwigEvaluateExtension.php

<?php
/**
 * Created by PhpStorm.
 * User: yezhou
 * Date: 2018/5/23
 * Time: 18:26
 */

/**
 * A twig extension that will add an "evaluate" filter, for dynamic evaluation.
 */
class TwigEvaluateExtension extends \Twig_Extension
{
    /**
     * Attaches the innervars filter to the Twig Environment.
     *
     * @return array
     */
    public function getFilters()
    {
        return array(
            'evaluate' => new \Twig_Filter_Method($this, 'evaluate', array(
                'needs_environment' => true,
                'needs_context' => true,
                'is_safe' => array(
                    'evaluate' => true
                )
            ))
        );
    }

    /**
     * This function will evaluate $string through the $environment, and return its results.
     *
     * @param array $context
     * @param string $string
     */
    public function evaluate(\Twig_Environment $environment, $context, $string)
    {
        $loader = $environment->getLoader();

        $parsed = $this->parseString($environment, $context, $string);

        $environment->setLoader($loader);
        return $parsed;
    }

    /**
     * Sets the parser for the environment to Twig_Loader_String, and parsed the string $string.
     *
     * @param \Twig_Environment $environment
     * @param array $context
     * @param string $string
     * @return string
     */
    protected function parseString(\Twig_Environment $environment, $context, $string)
    {
        $environment->setLoader(new \Twig_Loader_String());
        return $environment->render($string, $context);
    }

    /**
     * Returns the name of this extension.
     *
     * @return string
     */
    public function getName()
    {
        return 'evaluate';
    }
}

system/library/template/twig.php

$this->twig = new \Twig_Environment($loader, $config);
$this->twig->addExtension(new \TwigEvaluateExtension());

system/storage/modification/system/library/template/twig.php

$this->twig = new \Twig_Environment($loader, $config);
$this->twig->addExtension(new \TwigEvaluateExtension());

Twig Evaluate 实例

实例1

  • tpl
<div class="col-sm-10">
  <select name="<?php echo $pm; ?>_status" id="input-status" class="form-control">
    <?php $pm_status = $pm . '_status'; 
    if ($$pm_status) { ?>
    <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
    <option value="0"><?php echo $text_disabled; ?></option>
    <?php } else { ?>
    <option value="1"><?php echo $text_enabled; ?></option>
    <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
    <?php } ?>
  </select>
</div>
  • twig
<div class="col-sm-10">
  <select name="payment_{{ pm }}_status" id="input-status" class="form-control">
    {% set pm_status = pm ~ '_status' %}
    {% set pm_status_s = '{{ ' ~ pm_status ~ ' }}' %}
    {% if pm_status_s |evaluate %}
    <option value="1" selected="selected">{{ text_enabled }}</option>
    <option value="0">{{ text_disabled }}</option>
    {% else %}
    <option value="1">{{ text_enabled }}</option>
    <option value="0" selected="selected">{{ text_disabled }}</option>
    {% endif %}
  </select>
</div>

实例2

  • tpl
<div class="col-sm-10">
  <input type="text" name="<?php echo $pm; ?>_sort_order" value="<?php $pm_sortorder = $pm . '_sort_order';echo $$pm_sortorder; ?>" placeholder="<?php echo $entry_sort_order; ?>" id="input-sort-order" class="form-control" />
</div>
  • twig
<div class="col-sm-10">
  {% set pm_sortorder = pm ~ '_sort_order' %}
  {% set channel_sortorder = '{{ ' ~ pm_sortorder ~ ' }}' %}
  <input type="text" name="payment_{{ pm }}_sort_order" value="{{ channel_sortorder |evaluate }}" placeholder="{{ entry_sort_order }}" id="input-sort-order" class="form-control" />
</div>

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/opencart-twig-template-engine-add-evaluate-filter/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
OpenCart Twig模板引擎添加evaluate过滤器
参考:https://stackoverflow.com/questions/10945200/twig-variables-in-twig-variable Twig过滤器 Twig Extension: <?php /** * A twig extension that……
<<上一篇
下一篇>>
文章目录
关闭
目 录