fenom/docs/en/tags/macro.md

51 lines
985 B
Markdown
Raw Normal View History

2016-04-12 12:28:57 +03:00
Tag {macro}
===========
2013-02-21 22:47:24 +04:00
2016-04-12 12:28:57 +03:00
Macros are comparable with functions in regular programming languages.
They are useful to put often used HTML idioms into reusable elements to not repeat yourself.
### {macro}
2016-04-12 12:28:57 +03:00
Macros can be defined in any template using tag `{macro}`.
2013-02-21 22:47:24 +04:00
```smarty
2014-01-28 21:31:26 +04:00
{macro plus($x, $y, $z=0)}
2013-02-23 13:29:20 +04:00
x + y + z = {$x + $y + $z}
2013-02-21 22:47:24 +04:00
{/macro}
```
```smarty
2013-02-23 13:29:20 +04:00
{macro.plus x=$num y=100}
2013-02-21 22:47:24 +04:00
```
2014-04-09 20:04:38 +04:00
```smarty
2014-04-12 01:00:58 +04:00
{macro plus($x, $y, $z=0)}
2014-04-09 20:04:38 +04:00
...
2014-04-12 01:00:58 +04:00
{macro.plus x=2 y=$y}
2014-04-09 20:04:38 +04:00
...
{/macro}
```
2013-03-17 14:37:23 +04:00
2013-03-04 10:13:59 +04:00
### {import}
2016-04-12 12:28:57 +03:00
Macros can be defined in any template, and need to be "imported" before being used.
The above import call imports the "math.tpl" file (which can contain only macros, or a template and some macros),
and import the functions as items of the `macro` namespace.
2013-03-04 10:13:59 +04:00
```smarty
{import 'math.tpl'}
2016-04-12 12:28:57 +03:00
{macro.plus x=1 y=3}
2013-03-04 10:13:59 +04:00
```
2016-04-12 12:28:57 +03:00
Use another namespace instead of `macro`
2013-03-04 10:13:59 +04:00
```smarty
{import 'math.tpl' as math}
...
{math.plus x=5 y=100}
```
```smarty
{import [plus, minus, exp] from 'math.tpl' as math}
```