在fuelphp框架的twig中添加这个函数来设置自定义过滤器到这个文件fuel/packages/parser/classes/twig/fuel/extension.php
/**
* Returns a list of filters to add to the existing list.
*
* @return array An array of filters
*/
public function getFilters()
{
//custom by viyancs adding splite function because not found in twig documentation
return array(
'explode' => new Twig_Filter_Function('Class::explode_custom', array('pre_escape' => 'html', 'is_safe' => array('html'))),
);
}
并在另一个类中声明explode_custom
函数,在这种情况下,您可以使用相同的类或它所依赖的另一个类。
/*
* adding custom function for split character
* used for fuel/app/classes/twig/fuel/extension.php
* @params
* $string : this is twig variable or value example {{ test }}
* $split : this is split character example {{ test\tdata | split('\t') }} \t is split character
* @return
* array of explode
*
*/
public function explode_custom($string,$split)
{
$data = explode($split, $string);
return $data;
}
并使用它
{% set varStack = "stack[tab]overflow" | explode('\t') %}
{% for value in varStack %}
<li>{{ value }}</li>
{% endfor %}
结果是
stack
overflow