Smarty local variable concatenation with string

蹲街弑〆低调 提交于 2019-12-03 08:17:01

问题


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}.


回答1:


The way you are doing it is call the "short form" of assign, you just need to use the correct quoting mechanism:

 {$yes_src="`$const1`yes`$const2`"}

Use assign:

{assign var="yes_src" val="`$const1`yes`$const2`"}

Use 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 note If you find yourself using assign more 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.

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



回答2:


{ $yes_src = $variable|cat:"some string"|cat:$variable }



回答3:


Try this:

{capture assign=yes_src}{$const1}.'yes'.{$const2}{/capture}

And then use the new variable:

{$yes_src}


来源:https://stackoverflow.com/questions/11144406/smarty-local-variable-concatenation-with-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!