Time for action – Executing code from a template
Even though templates can't contain haXe code, they can make calls to so-called "template macros". Macros are defined by the developer and, just like data they are passed to the template.execute
function. In fact, they are passed exactly in the same way, but as the second parameter.
Calling them is quite easy, instead of surrounding them with ::
we will simply prefix them with $$
, we can also pass them as parameters inside parenthesis. So, let's take our preceding sample and add a macro to display the number of workers in a department.
First, let's add the function to our Main
class:
public static function displayNumberOfWorkers(resolve : String->Dynamic, department : Department) { return department.workers.length + " workers"; }
Note that the first argument that the macro will receive is a function that takes a String
and returns a Dynamic
. This function will allow you to retrieve the value of an expression in the context from...