Function Context

[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]
[loop start=[length]&end=1&advance=-1][getchars start=[index]&end=[index]][instring][/getchars][/loop]

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]

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].

User Contributed Notes

Scott Anderson - 
04/01/2003 at 15:30:08
An example WebDNA function that returns the requested system environment variable.  Windows platform only.

[function name=GetEnvironmentVar]
[listwords words=[DOS]Set
[showif [url][middle startafter=&endbefore==][word][/middle][/url]=[url][params_string][/url]]
[return][middle startafter==&endbefore=][word][/middle][/return]

CLASSPATH = [GetEnvironmentVar CLASSPATH]<br>
OS = [GetEnvironmentVar OS]<br>
SystemRoot = [GetEnvironmentVar SystemRoot]<br>

Donovan - 
09/04/2008 at 01:05:22
Just a quick note to say that there is a lot of good additional information, such as the use of [params_string] and  the [return][/return] elements, inside your WebDNA Lab tutorials (accessible through the WebDNA admin pages)


Copyright © 2008 WebDNA Software Corporation, Inc.