Sometimes it can be useful to run Lucee code which has been dynamically generated, or access variables which are dynamically generated. This can enable meta programming when the code or variable names cannot be known at compile time.
The following code sets a dynamic variable "test"
Its also possible to use the
getVariable() function to dynamically evaluate a variable name:
When doing more complex dynamic evaluations, you can make use of the evalute() function which will execute any Lucee expression as if it was normally executed. This example below produces the same output as above.
It is also possibly to dynamically access the members of a structure using array notation
Dynamically running Lucee tags or script that is more complex than simple expressions is usually not possible with evaluate(). In these situations, we can make use of Lucee's virtual filesystem to write and include dynamic code.
Then it is possible to dynamically create .cfm templates and include them. This example below compiles a template that outputs the time with a 50 milisecond delay, based on the number of iterations specified.
It is possible to pass to functions a dynamic set of arguments. This is useful when forwarding arguments from one function to another, or where there are a lot of arguments that need to be prepared.
When executing this example, it results in: