fenom/docs/en/tags/set.md

1.4 KiB

Tag {set}

The tag {set} is used for assigning template variables during the execution of a template.

{set $var=EXPR}
{set $var}
  ... any content ...
{/set}
{set $var|modifiers}
  ... any content ...
{/set}

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

{set $v = 5}
{set $v = "value"}

{set $v = $x+$y}
{set $v = 4}
{set $v = $z++ + 1}
{set $v = --$z}
{set $v = $y/$x}
{set $v = $y-$x}
{set $v = $y*$x-2}
{set $v = ($y^$x)+7}

Works this array too

{set $v = [1,2,3]}
{set $v = []}
{set $v = ["one"|upper => 1, 4 => $x, "three" => 3]}
{set $v = ["key1" => $y*$x-2, "key2" => ["z" => $z]]}

Getting function result into variable

{set $v = count([1,2,3])+7}

Fetch the output of the template into variable

{set $v}
    Some long {$text|trim}
{/set}

{set $v|escape} {* apply modifier to variable*}
    Some long {$text|trim}
{/set}

{add}

The tag {add} the same tag as {set} except that sets the value of the variable if it does not exist.

{add $var = 'value'}

instead of

{if $var is not set}
    {set $var = 'value'}
{/if}

{var}

Old name of tag {set}. Currently tag {var} the same tag as {set}.