smarty - 替换多个值而不是一个

bic*_*cle 0 smarty

文档:http://www.smarty.net/docsv2/en/language.modifier.replace.tpl

我想改变多个值,而不是替换单个值.在文档中,它说str_replace与php的str_replace相同.然后,我将如何明智地执行以下聪明的工作?

$letters = array('a', 'p');
$fruit   = array('apple', 'pear');
$text    = 'a p';
$output  = str_replace($letters, $fruit, $text);
Run Code Online (Sandbox Code Playgroud)

小智 8

smarty中的等价物是:

{assign "letters" array('a', 'p')}
{assign "fruit" array('apple', 'pear')}
{assign "text" 'a p'}
{$text|replace:$letters:$fruit}
Run Code Online (Sandbox Code Playgroud)

它提供与php相同的输出:

apearpearle pear
Run Code Online (Sandbox Code Playgroud)

但请注意" 在模板中分配变量实际上是将应用程序逻辑放入可以在PHP中更好地处理的表示 "(摘自http://www.smarty.net/docs/en/language.function.assign .tpl)