Expressions

Another very important feature of the pexeso framework is that it allows to specify PHP expressions directly in PXML component definition. The expression can be used almost anywhere between the PXML markup, even as attribute values.

The expressions may seem to be merely replacement of standard XML processing instructions of form:

<?php ...some PHP code ?>

But it is definitely not. Processing instructions defined in PXML just like in PHP page are executed when the PXML/PHP page is parsed, before the actual view is ever created. Expressions are executed at the special points of view's lifecycle when the view is created and complete so that any component of the view can be accessed by the expression.

Expression is executed out of any scope. For this reason there are always special properties defined in expression's scope:

$self
reference to PXML defined root object
$target
reference to object whose property is being assigned the expression
$event
defined only for event expressions and contains reference to the event dispatched

Syntax

PHP expressions in PXML are always enclosed in curly braces. Except event expressions they are always supposed to evaluate to value. Example of expression which result is assigned to object/component property.

<px:Button label="{gettext('OK')}"/>

Important: The expression should be always single PHP expression and the trailing semicolon can be omitted. Expression cannot not contain semicolon except as a last character of expression and should not contain curly braces other that the ones enclosing it, not even inside a literals.

Note: To use curly brace as literal in attribute values or anywhere in text you have to escape it by doubling it. Then it is not recognized as start or end of the expression and each double brace result in one brace in the output.

<h:p>The expression starts with "{{ and ends with }}".</h:p>

Event vs. Binding expressions

There are by default two kind of expression that works differently:

event expression
This expression is assigned to elemnt attribute that represents event and cannot be combined with another expression or text. It is simple expression that is evaluated when the associated event is dispatched and it's result it evaluates to is ignored. In scope of the expression is defined local varibale $event that contains the event object.
<px:Button label="OK" click="{$event->getTarget()->label='Revert'}"/>
In this example when the buton is clicked it changes it's label to "Revert" string.
binding expression
This expression can be used everywhere except name attribute or attribute that represents event of the component. It always evaluates to value. All binding expressions in the view are evaluated two times when view is created - once before execution phase and then just after execution phase. On view's postbacks they are executed once only after the view's execution.
static expression
Static expression is kind of expression that is evaluated only when view is initialized. During the rest of view's lifecycle is never evaluated again. This is a kind of optimization that is used especially to bind references to other PXML defined objects (i.e. reference of radio button to button group). Static expression is ordinary binding expression where the opening curly brace is immediately followed by the caret (^) character:
<px:Radio buttonGroup="{^$self->buttonGroup}"/>

When the binding expression is combined together with the text the value the expression evaluates to is converted to string.

<h:p>The selected colors are: {implode(",", array("Red", "Green"))}</h:p>