The name “candy” was retained for its legacy syntax, which is a syntax delimited by an opening %{
and a closing }%
, making it look like a candy (at least that’s how I saw it):
Your name is %{user.author}%, obviously!
Looking at how the web has evolved to date, it seems that people are more likely to prefer the variable syntax that was introduced by Jinja, Liquid, Mustache, and Twig. And so, I decided to support a similar syntax without having to incorporate it into a new extension to reduce maintenance frequency:
Your name is {{ user.author }}, obviously!
However, this feature is very limited and is meant to be read-only. The first token before the dot only accepts variable data in the form of an object or scalar. This extension does not accept array as the initial variable data to prevent outsiders from inspecting sensitive global variables such as $_COOKIE
, $_SERVER
, and $_SESSION
. Array data that is present after the first call to a property or method of an object is allowed.
If you want to manually provide some information from these sensitive variables using the syntax, you will need to add it to a new global variable and convert the data to an object or scalar so that it can be accepted by the parser:
$GLOBALS['server'] = (object) [
'host' => $_SERVER['HTTP_HOST'],
'port' => $_SERVER['SERVER_PORT']
];
$GLOBALS['server_host'] = $_SERVER['HTTP_HOST'];
You can then get the value by inserting this syntax into the page content:
{{ server.host }}
{{ server_host }}
Examples
Syntax | Description |
---|---|
{{ var }} | Prints the value of $var . If $var is an array, then this syntax will be left as it is. If $var is an object, then this syntax will tries to call $var->__invoke() or $var->__toString() method to return its value. |
{{& var }} | Prints the value of $var as raw HTML (by default, the value is always HTML-escaped). |
{{ var.var }} | Prints the value of $var->var or $var->var() or $var->__get('var') or $var->offsetGet('var') or $var->__call('var') . |
{{ var.var.var }} | Recursive calls as in the previous syntax. The final value will then be turned into a string. |
{{ var.var-var }} | Prints the value of $var->varVar or $var->varVar() or $var->__get('varVar') or $var->offsetGet('varVar') or $var->__call('varVar') . |
{{ var.varVar }} | Prints the value of $var->varVar or $var->varVar() or $var->__get('varVar') or $var->offsetGet('varVar') or $var->__call('varVar') . |
Writing literal syntax for presentation purposes can be done by separating part of the entire syntax with HTML elements, or by substituting the delimiters with their HTML entity characters:
<span>%{ var</span> }%
<span>{{ var</span> }}
%{ var }%
{{ var }}
0 Comments
No comments yet.