[function name=...&preparse=T/F][/function]
This new context enables the WebDNA programmer to call a previously defined block of WebDNA code.
Optional Tag Parameters:
· name=variablename - User defined name for the function.
The name if then used like a normal WebDNA tag.
· preparse=T/F - By default, the WebDNA code that defines
the function is stored 'raw' and executed later when the function is called.
But if you need to programmatically create the function definition using WebDNA,
then you can set 'preparse' to 'T'. This will force the WebDNA engine to first
parse the WebDNA in the function definition before storing it for later use.
Our first example creates a function named 'backwards' that will take a variable named 'instring' and display the characters of the string in reverse order. We use the following code...
[function name=Backwards]
[text]length=[countchars][instring][/countchars][/text]
[loop start=[length]&end=1&advance=-1][getchars start=[index]&end=[index]][instring][/getchars][/loop]
[/function]
Now the function is defined and stored for later use in the template.
To execute the new function, we use...
[Backwards instring=abcdef_12345]
Function names take precedence over WebDNA global tags. So it is possible to
'override' a WebDNA tag. For example, lets define a function named 'date' that
displays the date in bold text.
We use the following code...
[function name=Date]
[:global:Date]
[/function]
Note that in our function definition, we had to use explicit 'scoping' to access
the true WebDNA [date] tag. This is to prevent an infinite recursion when the
function code executes, i.e. keeps calling itself. You can learn more about
Scope and Scope Resolution in the 'Scope' tutorial.
Now when we use [date], our date function is called, instead of the global [date].