问题描述
我有包含这一行的 ..tpl 文件:
I have ..tpl file with this line:
{$error|replace:"pno":"personal number error"}
我需要修改它以便替换多个值,例如:
I need to modify it so multiple values will be replaced, sort of:
{$error|replace:"pno_error":"personal number error", "error_1":"1", "error_2":"2"}
我需要确保代码格式正确.我如何实现这一目标?
I need to make sure the code is correctly formed. How do I achieve this?
推荐答案
像这样
{assign "find" array('pno', 'error_1', 'error_2')}
{assign "repl" array('personal number error', 1, 2)}
{assign "text" 'error_1 and pno and error_2 are friends'}
{$text|replace:$find:$repl}
顺便说一句:不要通过控制器来做,在 tpl 文件中使用最终值?
btw: dont'cha rather do it through a controller and in tpl files use final values?
编辑
如何使它只替换完全匹配,例如如果$text 是'pno',则替换它,但如果$text 是'pnopno',则什么都不做?
在这种情况下,您可以使用正则表达式,但是,在数组中是不可能的(据我所知),您需要一步一步地进行,或者更确切地说是一个命令后的命令.
In that case, you can use regular expressions, however, it is not possible in an array (as far as I know) and you need to do it step by step, or rather a command after command.
关于替换pno
而不是pnopno
,您需要找出适合您需要的正则表达式.
Regarding of replacing pno
and not pnopno
, you need to figure out your own regular expression to suit your needs.
{assign "text" 'error_1 and pno and error_2 are friends'}
{$text|regex_replace:"/(\s)(pno)(\s)/":"personal number error"|regex_replace:"/(error_1)/":"1"|regex_replace:"/(error_2)/":"2"}
这篇关于smarty 替换多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!