php Smarty 局部变量与字符串连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11144406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Smarty local variable concatenation with string
提问by algorytmus
How to assign a local template variable with a string concatenated just like below:
如何使用连接的字符串分配本地模板变量,如下所示:
{$yes_src=const1.'yes'.const2}
to be used below in the code in the manner {$yes_src}.
以在代码下面的方式使用{$yes_src}。
回答by Chris Baker
The way you are doing it is call the "short form" of assign, you just need to use the correct quoting mechanism:
您这样做的方式是调用 的“简短形式” assign,您只需要使用正确的引用机制:
{$yes_src="`$const1`yes`$const2`"}
Use assign:
使用assign:
{assign var="yes_src" val="`$const1`yes`$const2`"}
Use cat:
使用cat:
{$const1|cat:"yes"}{$const2}
You can also simply put the variables next to one another without assigning it to a variable:
您也可以简单地将变量并排放置,而不将其分配给变量:
{$const1}yes{$const2}
... no variable needed.
...不需要变量。
A noteIf you find yourself using assignmore than rarely, you might have a misconception about the ideas of separating logic from presentation. Usually, concatenation and other variable work would be accomplished in PHP before the template is ever involved. The template's role is to just display the data, you should avoid creating or altering the data in the template.
的注意事项如果您发现自己使用assign超过很少,你可能有一个关于从表现分离逻辑的想法误解。通常,在涉及模板之前,连接和其他变量工作将在 PHP 中完成。模板的作用只是显示数据,您应该避免在模板中创建或更改数据。
Documentation
文档
- Smarty quotes - http://www.smarty.net/docs/en/language.syntax.quotes.tpl
- Smarty
assign- http://www.smarty.net/docs/en/language.function.assign.tpl - Smarty
cat- http://www.smarty.net/docsv2/en/language.modifier.cat
回答by Roko C. Buljan
{ $yes_src = $variable|cat:"some string"|cat:$variable }
回答by D-32
Try this:
尝试这个:
{capture assign=yes_src}{$const1}.'yes'.{$const2}{/capture}
And then use the new variable:
然后使用新变量:
{$yes_src}

