Besides taglibs MWT provides a simple yet powerful expression language that can be used in conjunction with tags to access data from the application logic out of the XHTML documents.
The expressions supported are easy to learn and to use but still very powerful and extendible. Expressions are written in the following syntax:
${...}The simplest form of an expression is just a variable name. To output the value of the variable planet you can use the following expression:
${planet}If the variable is an array you can access the array items like this:
${planets[2]}This works also for multi-dimensional arrays and for associative arrays so a more complex use can look like this:
${planets[3]['moons'][1]['size']}To access a property of an object you can use the dot-operator. The left side of the dot must be the variable containing the object and the right side of the dot must be the property name:
${planet.size}It's also possible to access nested properties:
${planet.moons[0].size}You can also do simple math or even complex math in expressions with or without variables:
${count * 2}
${(worms + 2) * 5 + (apples - 2) * 5}
${53 * 100 / 116}
Because Phable compiles those expressions into native PHP expressions you can use any operators which are supported by PHP.
Expressions can also be used to check for a certain condition. This is needed for some custom tags like the if tag of the STL_Core taglib:
${count == 200}This expression will return true or false depending on the current value of the variable count.
To use fixed strings in expressions you must enclose them in single quotes like in this example which compares the value of a variable with a string:
${name == 'John Doe'}To use a single quote inside the string you must escape it with a backslash:
${name == 'John Mc\'Doe'}In expressions you can call functions which are implemented by function taglibs. Like normal taglibs function taglibs have a prefix and are PHP class methods.
The table below sums up the function taglibs provided by MWT:
| Function prefix | Class name | File path | Description |
|---|---|---|---|
| fn | Phable_FT_STL_Functions | Phable/taglibs/STL/Functions.php | core functions |
| wp | Phable_FT_MWT_WP_Functions | Phable/taglibs/MWT/WP_Functions.php | wordpress functions |
| mwt | Phable_FT_MWT_Functions | Phable/taglibs/MWT/Functions.php | mwt API functions |
The mwt function taglib is very important as it provide access to MWT APIs. MWTs API are described in Chapter 1, paragraph Using MWT APIs in taglibs explain how to use MWT APIs in taglibs.
Here is an example which uses the function toUpperCase of the STL_Functions taglib:
If you run this example in your browser then you will get the output HELLO WORLD!!! on your screen.