Wikidot Include Techniques

Posted by James Kanjo on 09 Apr 2010 00:31, last edited by gerdami on 22 Sep 2010 10:18

Tags: include variables

rating: +6+x

This guide complements the two HowTos: Wikidot Includes for Beginners and Wikidot Includes for Experts.

Here we will show you how advanced include techniques can be used to automate tasks and create frameworks for Wikidot programs.

Default Values

With the utilization of two include pages, one can allow certain variables to retain default values if they are not defined. Recall in the very first example what happened whenever varC wasn't defined:

Our Page:

[[include inc:advanced
|varA=Banana
|varB=Split
]]

The result is the following:
Banana Split {$varC}

Notice how we get an ugly variable in the expressed page since we didn't define what it was. So, let's say that rather than having this weird variable show up, we want for **James!** (which renders as James!) to replace the varC if it isn't defined by the user. However, if defined, we want the defined version to appear in precedence.

This is fortunately possible with the aid of two includes. For this, we will use the typical inc:advanced plus a new page called inc:advanced-4 with the following on it:

[[include inc:advanced
|varA={$varA}
|varB={$varB}
|varC={$varC} |varC=**James!**
]]

Now we will use the above original include without varC clearly defined and track what is happening:

Step Rendered Code
Import inc:advanced-4 [[include inc:advanced |varA={$varA} |varB={$varB} |varC={$varC} |varC=**James!**]]
|varA=Banana [[include inc:advanced |varA=Banana |varB={$varB} |varC={$varC} |varC=**James!**]]
|varB=Split, [[include inc:advanced |varA=Banana |varB=Split, |varC={$varC} |varC=**James!**]]
Step Rendered Code
Import inc:advanced {$varA} {$varB} {$varC}
|varA=Banana Banana {$varB} {$varC}
|varB=Split, Banana Split, {$varC}
|varC={$varC} Banana Split, {$varC}
|varC=**James!** Banana Split, James!

And behold, we didn't define varC yet James! appears! But what if we wanted to define varC as "yum!"?

We'll examine this code:

[[include inc:advanced-4
|varA=Banana
|varB=Split,
|varC=yum!
]]
Step Rendered Code
Import inc:advanced-4 [[include inc:advanced |varA={$varA} |varB={$varB} |varC={$varC} |varC=**James!**]]
|varA=Banana [[include inc:advanced |varA=Banana |varB={$varB} |varC={$varC} |varC=**James!**]]
|varB=Split, [[include inc:advanced |varA=Banana |varB=Split, |varC={$varC} |varC=**James!**]]
|varC=yum! [[include inc:advanced |varA=Banana |varB=Split, |varC=yum! |varC=**James!**]]
Step Rendered Code
Import inc:advanced {$varA} {$varB} {$varC}
|varA=Banana Banana {$varB} {$varC}
|varB=Split, Banana Split, {$varC}
|varC=yum! Banana Split, yum!
|varC=**James!** Banana Split, yum!

Since the code is rendered top to bottom. left to right and variables are replaced before the next is processed, varC=**James!** wasn't able to replace a varC since it didn't exist after it was defined. The final note to make is that you can set default values for the other variables as well if wished.

For a simplified guide to default values, see Include with Defaults.

Related Articles

Authors

James KanjoJames Kanjo. Please visit his/her userPage.

Timothy FosterTimothy Foster. Please visit his/her userPage.


Related articles

Comments

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.