7. PHPTALES

PHPTALES is the expression syntax used inside tal, metal, phptal attributes. From above examples, you should have seen some PHPTALES examples (string:, php:, not:, ...). This chapter describes the usage of PHPTALES in templates.

The value of a TAL attribute may contain more than one expression (ex: tal:define), in which case each expression must be separated from the next one with a ';' character.

7.1. path:

This is the default modifier used in TAL expression when no other modifier is specified.

The following lines will give the same result :

<span tal:content="data/user/name"/>
<span tal:content="path:data/user/name"/>
<span>${data/user/name}</span>

Inside the template or inside expression strings, you can refer to a context variable using its path in the form ${path/to/my/variable}

<h1>${document/title}</h1>
<span tal:replace="string:welcome ${user/name}, 
this page has been readed ${page/countRead} times"/>

7.2. Conditional statements

As '<' and '>' should be removed from attribute expression. PHPTAL provides some equivalent good old text-comparison operators.

These statements will mostly appear in tal:condition attributes, and in php: expressions.

  • < : LT (less than)

  • > : GT (greater than)

  • <= : LE (less or equal)

  • >= : GE (greater or equal)

7.3. string:

Because expressions are separated by a ';' character, and because '$' marks the start of a path, you must use :

  • ';;' when you want to insert a real ';' character in a string,

  • '$$' when you want to insert a real '$' character in a string.

<span tal:replace="string:this is a $$100 page"/>
string:foo $bar baz       <!-- will replace $bar -->
string:foo $$bar baz      <!-- no interpolation -->
string:foo ; php:doFoo()  <!-- two different expressions -->
string:foo ;; php:doFoo() <!-- only string -->

7.4. php:

This expression evaluates what follows as a regular php expression except that '->' are replaced by dots '.' and variable names does not need to be prefixed with a dollar '$' sign.

A dot '.' separated from the rest of expression by spaces is assumed to be a concatenation sign.

php:htmlentities(foo)
php:'string ${varReplaced}'
php:'string ${some.path().to[0].var}'
php:NOT foo OR (bar GT baz)
php:a + b
php:array('a', 'b', 'c')
php:range(0, 90)
php:foo . a.b.c(e) . htmlentities(SomeClass::staticMethod()) 
php:SomeClass::ConstOfClass
php:SomeClass::$staticVar

php: should be used with care and won't be needed in 80% of your templates but sometimes you will need to invoke some special php method to be certain whether a user is logged in, or to retrieve specific complex data depending on some conditions, dynamically inside the template.

7.5. not:

This expression is a boolean one, useful in tal:condition statements.

<span tal:condition="not: logged">not logged</span>

7.6. exists:

This expression is a boolean expression, it will returns true if the path specified after it exists of not.

<span tal:condition="exists: user/preferences"
      tal:content="user/preferences">
  user preferences here if defined
</span>

It is important to keep in mind that using a path which doesn't exist will throw and exception. Thus, uncertain paths must be checked first.

7.7. default

This is not an expression but a keyword, allowing template designers to keep the content of a tag as an alternative value if an error occurs, or if something is not defined.

<span tal:define="myVar path/to/possible/var | default">
  default my var value
</span>

<span tal:content="some/var | other/path | default">
  no some/var and no other/path found here
</span>

<a href="unknown.html" title="Unknown page"
   tal:attributes="href item/href | default; title item/title | default"
   tal:content="item/title | default">Unknown page</a>

Above examples introduce the '|' character that allows the definition of alternatives for defines or prints.

7.8. structure

This is not an expression modifier but a keyword.

While printing variables inside PHPTAL templates, you will have noticed that PHPTAL encodes each variable to ensure the validity of the output document.

Sometimes, you may use HTML/XML variables which must be echoed as is.

<h1 tal:content="structure document/title"/>
<span tal:replace="structure document/content"/>

In above examples, we assume that $document->title and $document->content are variables containing preformated html which must be echoed as is.

7.9. Expression chains

An expression chain is a list of expressions separated by '|' characters.

While evaluating expressions separated by '|', PHPTAL will stop its evaluation when an expression value is not null and no error was raised while evaluating the expression.

As a string: expression is always true, string: always terminates an expression chain whatever expression may follow.

You can use php: expressions inside expression chains, like any other expression.

<h1 tal:content="page/title | page/alternativeTitle | default>
  untitled page
</h1>