[Spawn]Some Complex WebDNA[/Spawn]
To perform WebDNA simultaneously, put it inside a Spawn context. All WebDNA inside the Spawn context begins to execute immediately, and the remainder of the template is returned to the visiting browser immediately.
Not all WebDNA information is available inside a [Spawn] context, because often the outer template that created the spawn is already gone from memory, and thus its context information is gone as well. Only the following values are available inside the spawn, because a copy of them is made before creating the spawn context:
Example (normally you would put the following text into a .tpl file on your server and use a web browser to link to it):
The example above yields:
Before the spawn 1
After the spawn 3
Notice that the elapsedtime is very small, even though the loop inside the spawn could take several seconds. This is because your web browser sees the results of the template before the spawned WebDNA is finished.
Here are some common mistakes you should avoid:
[Loop start=1&end=10] [Spawn] [ShowIf [index]=5] stuff [/ShowIf] [/Spawn] [/Loop]
[Loop start=1&end=10] [Math]LoopIndex=[index][/Math] [Spawn] [ShowIf [LoopIndex]=5] stuff [/ShowIf] [/Spawn] [/Loop]
|Remember that Spawn might start executing long after the original template that created has gone away. Spawn has no idea what the value of [index] is, because that comes from the outer [Loop] context, which really 'belongs' to the now-gone outer template. The correct method is to create a math variable to hold the [index] value, because Spawn does keep a copy of all the math variables that were in existence when it was created.|
[Search blah] [FoundItems] [Spawn] [FieldFromDB] [/Spawn] [/FoundItems] [/Search]
[Search blah&max=10] [FoundItems] [Text]value=[FieldFromDB][/Text] [Spawn] [value] [/Spawn] [/FoundItems] [/Search]
|This is bad for two reasons. Similar to the first example, Spawn has no idea what the database field values are, because it is not truly inside the [FoundItems] context. Second, be very careful that you do no create too many spawns -- they can use a lot of memory, and in this case if the [FoundItems] is more than a dozen or so then web server performance can degrade considerably. The correct example limits the number of spawns, and also uses a text variable to hold the value of the database field.|