Инструменты пользователя

Инструменты сайта


limb3_2007_4:en:packages:macro:expressions

Expressions. How to render variable values in templates

Expressions syntax

Expressions - are special entities of macro template that have two main functions:

  1. to render variables (in such cases expressions are called output expressions):
    • {$percentage} - renders a variable from local data scope. The same as <?php echo $percentage; ?>
    • {$#percentage} - renders a variable from global (class) data scope ( renders attribute of the class). The same as <?php echo $this→percantage; ?>
    • {$item.title} - renders a variable extracted by some path. The same as <?php $var_001 = isset($item['title']) ? $item['title'] : «»; echo $var_001; ?>
  2. as a part of macro tag attribute value
    • {{input id=«title_{$index}»}} - so called «composite attribute value».

You can also apply one or several filters (other name is «formatters») to modify expression value or apply some special formatting.

So, expressions has the following syntax: {$Expression[|filter1|filterN]}

  • {$…} - sign of expression.
  • Expression - variable name or path.
  • filter1, filterN - applied filters. Filters are separated with vertical line.

See more about filters.

Path-based expressions

Expressions like {$item.title} - we call «path-based expressions». Path to variable may has several elements separated with dots. MACRO splits such expressions into components and generates approximately the following code for each component:

if((is_array($sub_var1) || $sub_var1 instanceof ArrayAccess) && isset($sub_var1[$stepN])) 
  $sub_var2 = $sub_var1[$step2]; 
else 
  $sub_var2 = "";

$sub_varN - is a temporary variable, and $stepN - one of the elements of variable path. This means what all steps before final element should be arrays or support ArrayAccess interface. If expression can't proceed any deeper into path, an empty string will be used as expression value.

Expressions with method calls

Expressions are also supports method calls or accessing object attributes directly. Just use arrow symbol as the follows:

{$#some_object->someMethod()}
{$#some_object->attr1}

You can pass any arguments into method:

{$#some_object->someMethod("aaa", $#foo)}

You can continue path-based expression after method call, for example:

{$#book->getAuthor().full_name}

The last example will be compiled into something like this:

$temp_var = $this->book->getAuthor();
if((is_array($temp_var) || $temp_var instanceof ArrayAccess) && isset($temp_var['full_name'])) 
  $sub_var = temp_var['full_name']; 
else 
  $sub_var = "";

When {{macro}} applies default html filter to output expressions

You should know what macro applies html filter (an alias for htmlspecialchars()) for all output expressions without other filters. If you don't need html filter to be applied to some particular expression you can use raw filter that does nothing but cancels html-filter applying.

Her is a list of different situations:

  • {$title} - html-filter will be applied
  • {$title|raw} - html-filter will no be applied since raw filter is used
  • {$title|html} - html-filter will be applied since used explicitly
  • {$title|trim} - html-filter will not be applied since other filters are used
  • {$title|trim|html} - html-filter will be applied since used explicitly

Обсуждение

Ваш комментарий. Вики-синтаксис разрешён:
   __    ___   _      __   _  __   ___ 
  / /   / _ \ | | /| / /  / |/ /  / _ )
 / /__ / ___/ | |/ |/ /  /    /  / _  |
/____//_/     |__/|__/  /_/|_/  /____/
 
limb3_2007_4/en/packages/macro/expressions.txt · Последние изменения: 2010/11/10 10:02 (внешнее изменение)