29

I want to assign the value obtained from the concatenation of these two variables with a string.

{assign var="url" value="{$WS_PATH}aircraft_images/{$images[i].image}"}

Please let me know how can we do this in smarty.

R3tep
  • 12,512
  • 10
  • 48
  • 75
user1163513
  • 4,087
  • 7
  • 24
  • 25
  • Does this answer your question? [Smarty local variable concatenation with string](https://stackoverflow.com/questions/11144406/smarty-local-variable-concatenation-with-string) – Chris Baker Mar 17 '21 at 13:29

3 Answers3

47

One of these should work:

{assign var="url" value=$WS_PATH|cat:"aircraft_images/"|cat:$images[i].image}

Or

{assign var="url" value="`$WS_PATH`aircraft_images/`$images[i].image`"}

I am not sure if the $images[i].image will be parsed correctly, you may have to {assign} it to another variable first

periklis
  • 10,102
  • 6
  • 60
  • 68
8

You've used assign properly.

A simplified example could look like this:

yourphpfile.php:

$tpl = new Smarty;
$tpl->assign('var1','Hello');
$tpl->assign('var2','World');
$tpl->display('yourtemplate.tpl');

yourtemplate.tpl:

...
<body>
{assign var="url" value="{$var1} - and - {$var2}"}
{$url}
</body>

...will result to the output:

Hello - and - World
Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • This actually works, but where is it documented O.o ? – jave.web Aug 28 '17 at 19:42
  • @jave.web Have a look at the [variable examples here in the manual](https://www.smarty.net/docs/en/language.syntax.variables.tpl), they cover most of the use cases. – Bjoern Aug 28 '17 at 20:33
  • well that hardly shows {} are used to address ambiguities and to concatenate in var/index naming, there is not this case ... x) – jave.web Aug 30 '17 at 00:02
5

such an expression will do the trick:

{$product1_photo = "{$smarty.const.IMG_URL}/{$pInfo.PhotoName}"}
Alp Altunel
  • 3,324
  • 1
  • 26
  • 27