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

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

Twig过滤器

Twig Extension:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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:

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

In the template:

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

OpenCart Twig Evaluate

system/library/template/TwigEvaluateExtension.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?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

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

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

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

Twig Evaluate 实例

实例1

  • tpl
1
2
3
4
5
6
7
8
9
10
11
12
<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
1
2
3
4
5
6
7
8
9
10
11
12
13
<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
1
2
3
<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
1
2
3
4
5
<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>

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :