2014-05-08 12:56:37 +04:00
Syntax
======
2013-02-01 19:13:07 +04:00
2014-05-08 12:56:37 +04:00
Fenom implements [Smarty ](http://www.smarty.net/ ) syntax with some improvements.
All Fenom tags enclosed in the delimiters `{` and `}` , for example `{var $five = 5}` .
If you wanna leave delimiters as is in the template use [special statements or tags ](#ignoring-delimiters ).
2014-05-14 17:07:48 +04:00
**Note**
2014-05-08 12:56:37 +04:00
Fenom implements [Smarty ](http://www.smarty.net/ ) syntax but not implements Smarty tags, however, some tags very similar.
But not so bad, Fenom has the [extras ](https://github.com/bzick/fenom-extra ) that make Fenom like Smarty.
2013-05-30 20:00:52 +04:00
2013-06-19 10:56:28 +04:00
## Variable
2014-05-08 12:56:37 +04:00
Variables in Fenom can be either displayed directly or used as arguments for functions, attributes and modifiers,
inside conditional expressions, etc.
2014-05-14 17:07:48 +04:00
### Use variables
2014-05-08 12:56:37 +04:00
Next example uses simple variables `$user_id` ans `$user_name`
```smarty
< div class = "user" > Hello, < a href = "/users/{$user_id}" > {$user_name}< / a > .< / div >
```
Example outputs next HTML code:
```html
< div class = "user" > Hello, < a href = "/users/17" > Bzick< / a > .< / div >
```
2014-07-29 13:22:46 +04:00
You can also reference associative array variables by specifying the key after a dot `.` symbol or paste key name into square brackets, as in PHP.
2014-05-08 12:56:37 +04:00
```smarty
< div class = "user" > Hello, < a href = "/users/{$user.id}" > {$user.name}< / a > .< / div >
```
2014-05-14 17:07:48 +04:00
`{$user.id}` and `{$user['id']}` are same:
```smarty
2014-05-19 10:13:29 +04:00
< div class = "user" > Hello, < a href = "/users/{$user['id']}" > {$user['name']}< / a > .< / div >
2014-05-14 17:07:48 +04:00
```
2014-05-08 12:56:37 +04:00
2014-07-29 13:22:46 +04:00
Properties of objects assigned from PHP can be referenced by specifying the property name after the `->` symbol:
2014-05-08 12:56:37 +04:00
```smarty
< div class = "user" > Hello, < a href = "/users/{$user->id}" > {$user->name}< / a > .< / div >
```
2014-07-29 13:22:46 +04:00
Methods of objects defined in PHP can be invoked by specifying the method name after the `->` symbol and use parenthesis with arguments:
2014-05-08 12:56:37 +04:00
```smarty
< div class = "user" > Hello, < a href = "/users/{$user->getId()}" > {$user->getName()}< / a > .< / div >
```
*Note*
Be careful, Fenom do not checks existence of the method before invoke.
2014-05-14 17:07:48 +04:00
To avoid the problem class of the object have to define method `__call` , which throws an exception, etc.
Also you can prohibit method call in [settings ](./docs/configuration.md ).
2014-05-08 12:56:37 +04:00
2014-07-29 13:22:46 +04:00
Below is complex example:
2014-05-08 12:56:37 +04:00
```smarty
{$foo.bar.baz}
{$foo.$bar.$baz}
2014-05-14 17:07:48 +04:00
{$foo[5].baz}
{$foo[5].$baz}
2014-05-08 12:56:37 +04:00
{$foo.bar.baz[4]}
{$foo[ $bar.baz ]}
2014-05-14 17:07:48 +04:00
{$foo[5]}
{$foo.5}
2013-02-09 10:59:08 +04:00
{$foo.bar}
2013-02-01 19:13:07 +04:00
{$foo.'bar'}
{$foo."bar"}
{$foo['bar']}
{$foo["bar"]}
2013-02-09 10:59:08 +04:00
{$foo.$bar}
2013-02-01 19:13:07 +04:00
{$foo[$bar]}
2013-02-09 10:59:08 +04:00
{$foo->bar}
2013-08-12 20:14:35 +04:00
{$foo->bar.buz}
2014-05-14 17:07:48 +04:00
{$foo->bar.buz[ $bar->getId("user") ]}
{$foo->bar(5)->buz(5.5)}
2013-08-12 20:14:35 +04:00
```
### System variable
2014-07-29 13:22:46 +04:00
Unnamed system variable starts with `$.` and allows access to global variables and template information:
2013-08-12 20:14:35 +04:00
* `$.get` is `$_GET` .
* `$.post` is `$_POST` .
* `$.cookie` is `$_COOKIE` .
* `$.session` is `$_SESSION` .
* `$.globals` is `$GLOBALS` .
* `$.request` is `$_REQUEST` .
* `$.files` is `$_FILES` .
* `$.server` is `$_SERVER` .
* `$.env` is `$_ENV` .
* `$.tpl.name` returns current template name.
* `$.tpl.schema` returns current schema of the template.
* `$.version` returns version of the Fenom.
* `$.const` paste constant.
```smarty
{if $.get.debug? & & $.const.DEBUG}
...
{/if}
2013-02-01 19:13:07 +04:00
```
2014-07-29 13:22:46 +04:00
### Variable operations
2013-02-19 09:51:33 +04:00
2014-07-29 13:22:46 +04:00
Fenom supports math, logic, comparison, containment, test, concatenation operators...
2013-06-19 10:56:28 +04:00
2014-07-29 13:22:46 +04:00
todo
2014-01-28 21:31:26 +04:00
2014-07-29 13:22:46 +04:00
See all [operators ](./operators.md )
2014-01-28 21:31:26 +04:00
2013-06-19 10:56:28 +04:00
### Set variable
2013-02-01 19:13:07 +04:00
```smarty
2013-06-19 10:56:28 +04:00
{var $foo = "bar"}
{var $foo = "bar"|upper} {* apply modifier *}
{var $foo = 5}
2013-02-01 19:13:07 +04:00
{var $foo = $x + $y}
{var $foo = $x.y[z] + $y}
2013-06-19 10:56:28 +04:00
{var $foo = strlen($a)} {* work with functions *}
2013-02-01 19:13:07 +04:00
{var $foo = myfunct( ($x+$y)*3 )}
2013-06-19 10:56:28 +04:00
{var $foo.bar.baz = 1} {* multidimensional value support *}
{var $foo = $object->item->method($y, 'named')} {* work with object fine *}
2013-02-01 19:13:07 +04:00
```
2013-06-19 10:56:28 +04:00
Using block tag
2013-02-01 19:13:07 +04:00
```smarty
2013-06-19 10:56:28 +04:00
{var $foo}
content {$text|truncate:30}
{/var}
{var $foo|truncate:50} {* apply modifier to content *}
content {$text}
{/var}
2013-02-19 09:51:33 +04:00
```
2013-06-19 10:56:28 +04:00
Set array
2013-02-19 09:51:33 +04:00
```smarty
2013-06-19 10:56:28 +04:00
{var $foo = [1, 2, 3]} numeric array
{var $foo = ['y' => 'yellow', 'b' => 'blue']} associative array
{var $foo = [1, [9, 8], 3]} can be nested
{var $foo = [1, $two, $three * 3 + 9]}
2013-02-24 14:02:18 +04:00
{var $foo = [$a, $d.c, $a + $f]}
2013-06-19 10:56:28 +04:00
{var $foo = ['y' => 'yellow', $color|upper => $colors[ $color ]}
{var $foo = [1, [$parent, $a->method()], 3]}
2013-02-01 19:13:07 +04:00
```
2013-06-19 10:56:28 +04:00
See also [{var} ](./tags/var.md ) documentation.
2013-02-01 19:13:07 +04:00
2013-06-19 10:56:28 +04:00
## Scalar values
2013-02-01 19:13:07 +04:00
2013-06-19 10:56:28 +04:00
### Strings
2013-02-01 19:13:07 +04:00
2013-06-19 10:56:28 +04:00
When the string in double quotation marks, all the expressions in the string will be run.
2014-02-14 15:55:36 +04:00
The result of expressions will be inserted into the string instead it.
2013-02-01 19:13:07 +04:00
```smarty
2013-02-19 09:51:33 +04:00
{var $foo="Username"}
{var $user.name="Username"}
2013-06-19 10:56:28 +04:00
{"Hi, $foo"} outputs "Hi, Username"
{"Hi, {$foo}"} outputs "Hi, Username"
{"Hi, {$user.name}"} outputs "Hi, Username"
{"Hi, {$user.name|up}"} outputs "Hi, USERNAME"
{"Hi, {$user->getName(true)}"} outputs Hi, Username
2013-02-19 09:51:33 +04:00
{var $message = "Hi, {$user.name}"}
2013-02-01 19:13:07 +04:00
```
2013-06-19 10:56:28 +04:00
but if use single quote any template expressions will be on display as it is
2013-02-19 09:51:33 +04:00
```smarty
2013-06-19 10:56:28 +04:00
{'Hi, $foo'} outputs 'Hi, $foo'
{'Hi, {$foo}'} outputs 'Hi, {$foo}'
{'Hi, {$user.name}'} outputs 'Hi, {$user.name}'
{'Hi, {$user.name|up}'} outputs "Hi, {$user.name|up}"
2013-02-19 09:51:33 +04:00
```
2014-07-29 13:22:46 +04:00
### Integers
Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).
To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x.
To use binary notation precede the number with 0b.
2013-02-19 09:51:33 +04:00
```smarty
2014-07-29 13:22:46 +04:00
{var $a = 1234} decimal number
{var $a = -123} a negative number
{var $a = 0123} octal number (equivalent to 83 decimal)
{var $a = 0x1A} hexadecimal number (equivalent to 26 decimal)
{var $a = 0b11111111} binary number (equivalent to 255 decimal)
```
**Note**
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed).
64-bit platforms usually have a maximum value of about 9223372036854775807
**Warning**
If an invalid digit is given in an octal integer (i.e. 8 or 9), the rest of the number is ignored.
### Floating point numbers
Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes:
```smarty
{var $a = 1.234}
{var $b = 1.2e3}
{var $c = 7E-10}
2013-02-19 09:51:33 +04:00
```
2014-05-14 17:07:48 +04:00
## Modifiers
2013-02-01 19:13:07 +04:00
2014-07-29 13:22:46 +04:00
Variable modifiers can be applied to variables, custom functions or strings.
To apply a modifier, specify the value followed by a | (pipe) and the modifier name.
A modifier may accept additional parameters that affect its behavior.
These parameters follow the modifier name and are separated by a : (colon).
2013-02-01 19:13:07 +04:00
```smarty
2013-02-19 09:51:33 +04:00
{var $foo="User"}
2013-06-20 10:36:35 +04:00
{$foo|upper} outputs "USER"
{$foo|lower} outputs "user"
{"{$foo|lower}"} outputs "user"
{"User"|lower}} outputs "user"
{$looong_text|truncate:80:"..."} truncate the text to 80 symbols and append < continue > symbols, like "..."
2013-02-01 19:13:07 +04:00
{$looong_text|lower|truncate:$settings.count:$settings.etc}
2013-06-20 10:36:35 +04:00
{var $foo="Ivan"|upper} sets $foo value "USER"
2013-02-01 19:13:07 +04:00
```
2013-06-19 10:56:28 +04:00
[List of modifiers ](./main.md#modifiers )
2013-02-01 19:13:07 +04:00
2014-05-14 17:07:48 +04:00
## Tags
2013-02-01 19:13:07 +04:00
2014-02-14 15:55:36 +04:00
Basically, tag seems like
2013-02-01 19:13:07 +04:00
```smarty
{FUNCNAME attr1 = "val1" attr2 = $val2}
```
2014-02-14 15:55:36 +04:00
Tags starts with name and may have attributes
2013-02-19 09:51:33 +04:00
Это общий формат функций, но могут быть исключения, например функция [{var} ](./tags/var.md ), использованная выше.
2013-02-01 19:13:07 +04:00
```smarty
{include file="my.tpl"}
{var $foo=5}
{if $user.loggined}
Welcome, < span style = "color: red" > {$user.name}!< / span >
{else}
Who are you?
{/if}
```
2013-02-19 09:51:33 +04:00
В общем случае аргументы принимают любой формат переменных, в том числе результаты арифметических операций и модификаторов.
2013-02-01 19:13:07 +04:00
```smarty
{funct arg=true}
{funct arg=5}
{funct arg=1.2}
{funct arg='string'}
{funct arg="string this {$var}"}
{funct arg=[1,2,34]}
{funct arg=$x}
{funct arg=$x.c}
```
```smarty
{funct arg="ivan"|upper}
{funct arg=$a.d.c|lower}
```
```smarty
{funct arg=1+2}
{funct arg=$a.d.c+4}
{funct arg=($a.d.c|count+4)/3}
```
2014-07-29 13:22:46 +04:00
## Static method support
By default static methods are allowed in templates
```smarty
{Lib\Math::multiple x=3 y=4} static method as tag
{Lib\Math::multiple(3,4)} inline static method
{12 + Lib\Math::multiple(3,4)}
{12 + 3|Lib\Math::multiple:4} static method as modifier
```
You may disable call static methods in template, see in [security options ](./settings.md ) option `deny_static`
2013-02-01 19:13:07 +04:00
2014-07-29 13:22:46 +04:00
## Ignoring template code
2013-02-01 19:13:07 +04:00
2014-07-29 13:22:46 +04:00
It is sometimes desirable or even necessary to have ignore sections it would otherwise parse.
A classic example is embedding Javascript or CSS code in a template.
The problem arises as those languages use the `{` and `}` characters which are also the default delimiters for Fenom.
Fenom has several solutions:
1. Uses block tag `{ignore} {/ignore}` . Anything within `{ignore} {/ignore}` tags is not interpreted, but displayed as-is.
2. The `{` and `}` braces will be ignored so long as they are surrounded by white space.
3. Uses tag option `:ignore` for block tag. В с е Fenom теги внутри блока будут проигнорированны
2013-02-01 19:13:07 +04:00
2014-05-14 17:07:48 +04:00
Example:
2013-02-01 19:13:07 +04:00
```smarty
2013-02-09 10:59:08 +04:00
{ignore}
2013-02-01 19:13:07 +04:00
< style >
h1 {font-size: 24px; color: #F00 ;}
< / style >
2013-02-15 12:17:23 +04:00
{/ignore}
2013-02-01 19:13:07 +04:00
< script >
(function (text) {
var e = document.createElement('P');
e.innerHTML = text;
document.body.appendChild(e);
})('test');
2014-05-14 17:07:48 +04:00
{if:ignore $cdn.yandex}
var item = {cdn: "//yandex.st/"};
2014-02-22 20:34:53 +04:00
{/if}
2014-05-14 17:07:48 +04:00
< / script >
2013-02-01 19:13:07 +04:00
```
2014-02-22 20:34:53 +04:00
Outputs
2013-02-01 19:13:07 +04:00
```html
< style >
h1 {font-size: 24px; color: #F00 ;}
< / style >
< script >
(function (text) {
var e = document.createElement('P');
e.innerHTML = text;
document.body.appendChild(e);
})('test');
2014-05-14 17:07:48 +04:00
var item = {cdn: "//yandex.st/"};
2013-02-01 19:13:07 +04:00
< / script >
2013-02-19 09:51:33 +04:00
```
2013-02-24 14:02:18 +04:00
### Whitespaces
2013-02-19 09:51:33 +04:00
2014-07-29 13:22:46 +04:00
Tags to allow any number of spaces
2013-02-19 09:51:33 +04:00
```smarty
{include 'control.tpl'
2014-02-22 20:34:53 +04:00
$options = $list
$name = $cp.name
$type = 'select'
isolate = true
disable_static = true
2013-02-19 09:51:33 +04:00
}
{foreach [
"one" => 1,
"two" => 2,
"three" => 3
2013-05-30 20:00:52 +04:00
] as $key => $val}
2013-02-19 09:51:33 +04:00
{$key}: {$val}
{/foreach}
2014-02-22 20:34:53 +04:00
```
2014-04-09 18:03:49 +04:00
### Tag options
2014-02-22 20:34:53 +04:00
2014-07-29 13:22:46 +04:00
TODO
2014-02-22 20:34:53 +04:00
| name | code | type | description |
| ------- | ---- | ----- | ------------ |
2014-05-14 17:07:48 +04:00
| strip | s | block | enable `strip` option for a block of the template |
2014-04-24 00:04:27 +04:00
| raw | a | any | ignore escape option |
| escape | e | any | force escape |
| ignore | i | block | ignore Fenom syntax |
2014-02-22 20:34:53 +04:00
```smarty
2014-04-09 18:03:49 +04:00
{script:ignore} ... {/script}
2014-04-24 00:04:27 +04:00
{foreach:ignore:strip ...} ... {/foreach}
```