How to combine two string in Smarty?

19,276

Solution 1

I have only used smarty a little but I think if you surround your concatenation with backticks then it will evaluate them properly. Example below:

{my_function(`$test.'a1'`)} 

There is also the assign built in function which may also be useful: http://www.smarty.net/docsv2/en/language.custom.functions.tpl#language.function.assign

Finally, if all else fails do the concat in php and assign it to a single variable and passing that to the template.


Edit, ignore the above suggestions, I think you should be using the following syntax:

{my_function var="`$test`a1"}

http://www.smarty.net/docsv2/en/language.syntax.quotes.tpl

Solution 2

If you're doing it passing into a function you can do a capture or assign

{capture assign="parameter"}{$test}a1{/capture} {my_function($parameter)}

{assign var="parameter" value=$test|cat:"a1"} {my_function($parameter)}

I have not tried using a modifier on the parameter to a function. But you could try it out. Also since its a custom smarty function you could add a third optional parameter and concatenate the values in side the function.

<?php

function smarty_function_my_function($params, $smarty) {
   $input = join('', $params);
}
Share:
19,276
user861587
Author by

user861587

Updated on June 14, 2022

Comments

  • user861587
    user861587 almost 2 years

    Why doesn't this work in Smarty?

    {my_function($test.'a1')}
    

    It's showing following error:

    Fatal error: Uncaught exception 'SmartyCompilerException' with message
    'Syntax Error in template "test.tpl" on line 1 "{my_function($test.'a1')}"
    Unexpected "'a1'", expected one of: "{" , "$" , "identifier" , INTEGER' in...