fenom/docs/en/tags/var.md

65 lines
1.1 KiB
Markdown
Raw Normal View History

2013-08-07 14:36:50 +04:00
Tag {var}
=========
2013-02-07 20:04:00 +04:00
2013-08-07 14:36:50 +04:00
The tag {var} is used for assigning template variables during the execution of a template.
2013-02-07 20:04:00 +04:00
```smarty
{var $var=EXPR}
```
2013-03-04 10:13:59 +04:00
```smarty
{var $var}
... any content ...
{/var}
```
2013-05-30 20:00:52 +04:00
```smarty
{var $var|modifiers}
... any content ...
{/var}
```
2013-08-07 14:36:50 +04:00
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.
2013-02-07 20:04:00 +04:00
```smarty
{var $v = 5}
{var $v = "value"}
{var $v = $x+$y}
2013-03-04 10:13:59 +04:00
{var $v = 4}
2013-02-07 20:04:00 +04:00
{var $v = $z++ + 1}
{var $v = --$z}
{var $v = $y/$x}
{var $v = $y-$x}
{var $v = $y*$x-2}
{var $v = ($y^$x)+7}
2013-08-07 14:36:50 +04:00
```
2013-02-07 20:04:00 +04:00
2013-08-07 14:36:50 +04:00
Creating array
2013-02-07 20:04:00 +04:00
2013-08-07 14:36:50 +04:00
```smarty
2013-02-07 20:04:00 +04:00
{var $v = [1,2,3]}
{var $v = []}
{var $v = ["one"|upper => 1, 4 => $x, "three" => 3]}
{var $v = ["key1" => $y*$x-2, "key2" => ["z" => $z]]}
2013-08-07 14:36:50 +04:00
```
2013-02-07 20:04:00 +04:00
2013-08-07 14:36:50 +04:00
Getting function result into variable
2013-02-07 20:04:00 +04:00
2013-08-07 14:36:50 +04:00
```smarty
2013-02-07 20:04:00 +04:00
{var $v = count([1,2,3])+7}
2013-08-07 14:36:50 +04:00
```
Collect the output of the template into a variable
2013-03-04 10:13:59 +04:00
2013-08-07 14:36:50 +04:00
```smarty
2013-03-04 10:13:59 +04:00
{var $v}
Some long {$text|trim}
{/var}
2013-06-18 18:50:24 +04:00
{var $v|escape} {* apply modifier to variable*}
Some long {$text|trim}
{/var}
2013-02-07 20:04:00 +04:00
```