43

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

By the way I am looking for a job as PHP developer :)

1

3 Answers 3

92

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

1
  • 3
    Smarty3 also allows {$yes_src = "{$cont1}yes{$const2}"}
    – rodneyrehm
    Jun 22, 2012 at 14:28
14
{ $yes_src = $variable|cat:"some string"|cat:$variable }
1
  • 1
    Valueable when one need concat string containing quotes because back ticks dos not work with apostrophe. {assign 'myLoc' '<span class="my">|cat:{$smarty.config.LOCALE_STR}|cat:"</span>"}
    – TMa
    Jan 13, 2015 at 22:33
11

Try this:

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

And then use the new variable:

{$yes_src}
1
  • This is golden!
    – Dgloria
    Nov 25, 2021 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.