fenom/docs/en/tags/switch.md

46 lines
1000 B
Markdown
Raw Permalink Normal View History

2013-02-13 18:51:53 +04:00
Tag {switch}
============
2013-09-05 03:35:02 +04:00
The `{switch}` tag is similar to a series of `{if}` statements on the same expression.
In many occasions, you may want to compare the same variable (or expression) with many different values,
and execute a different piece of code depending on which value it equals to. This is exactly what the `{switch}` tag is for.
Tag `{switch}` accepts any expression. But `{case}` accepts only static scalar values or constants.
2013-02-20 18:02:18 +04:00
```smarty
{switch <condition>}
{case <value1>}
...
2013-08-30 10:26:17 +04:00
{case <value2>, <value3>, ...}
...
{case <value3>}
2013-02-20 18:02:18 +04:00
...
2014-05-14 17:07:48 +04:00
{case default, <value1>}
2013-02-20 18:02:18 +04:00
...
{/switch}
2013-08-30 10:26:17 +04:00
```
For example,
```smarty
{switch $type}
{case 'new'}
It is new item
{case 'current', 'new'}
It is new or current item
{case 'current'}
It is current item
2014-02-27 16:30:44 +04:00
{case 'new', 'newer'}
2013-08-30 10:26:17 +04:00
It is new item, again
2014-05-14 17:07:48 +04:00
{case default}
2013-08-30 10:26:17 +04:00
I don't know the type {$type}
{/switch}
```
2013-09-05 03:35:02 +04:00
if `$type = 'new'` then template output
2013-08-30 10:26:17 +04:00
```
It is new item
It is new or current item
It is new item, again
2013-02-20 18:02:18 +04:00
```