How to combine two string in Smarty?

user861587 picture user861587 · Sep 10, 2011 · Viewed 19k times · Source

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

Answer

grahamrb picture grahamrb · Sep 10, 2011

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