smarty 替换多个值 [英] smarty replace multiple values

查看:58
本文介绍了smarty 替换多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含这一行的 ..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',则什么都不做?

how to make it to replace the exact match only, for example if the $text is 'pno', then replace it, but if the $text is 'pnopno', then do nothing?

在这种情况下,您可以使用正则表达式,但是,在数组中是不可能的(据我所知),您需要一步一步地进行,或者更确切地说是一个命令后的命令.

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 替换多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆