2

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...
user861587
  • 69
  • 1
  • 2
  • 9

4 Answers4

4

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

grahamrb
  • 2,159
  • 2
  • 15
  • 22
3

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);
}
Logan Bailey
  • 7,039
  • 7
  • 36
  • 44
0

I have found this code and works fine for me

Location smarty\plugins\function.concat.php

<?php
function smarty_function_concat($params){
    return implode('', $params);
}
?>

On .tpl code:

{concat 1="string1" 2="second" 3="an other string"}

Hamidreza
  • 1,825
  • 4
  • 29
  • 40
0

It is because Smarty does not understand PHP syntax. It uses it's own syntax as described here and quickly compared to PHP syntax here. (I have included the latter link to reinforce the point -- Smarty syntax is Smarty syntax.)

See the section on embedding vars in double quotes which should given an idea of how to accomplish the task. (Hint: for this case back-ticks must be used inside the quotes).

Happy coding.