Wikidot Includes: For Experts

Posted by GoVegan on 18 Mar 2010 23:45, last edited by GoVegan on 09 May 2010 06:06

Tags: include variable

rating: +9+x

Through understanding how the Wikidot engine processes includes, we are able to create highly sophisticated elements of code:

  1. Imports the page specified
  2. Replaces Variables in the included page only, according to the the definitions in the [[include]] block in the order:
    • From left-to-right
    • From top-to-bottom

This is difficult to grasp at first, so lets put this into practice.

Ordering of Processes

inc:advanced

{$varA} {$varB} {$varC}

Our Page:

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

So the first thing that the Wikidot engine will do is import the page inc:advanced then assign values to the Variables, in the order of left-to-right, top-to-bottom.

Step Rendered Code
Import inc:advanced

{$varA} {$varB} {$varC}

|varA=Banana

Banana {$varB} {$varC}

|varB=Split

Banana Split {$varC}

Now lets see what happens if we place everything on one line:

Our Page:

[[include inc:advanced |varA=Banana |varB=Split]]
Step Rendered Code
Import inc:advanced

{$varA} {$varB} {$varC}

|varA=Banana

Banana {$varB} {$varC}

|varB=Split

Banana Split {$varC}

The same results.

Temporary Variables

Because of the way the Wikidot engine processes includes, we can actually create our own Temporary Variables within the include code:

Our Page:

[[include inc:advanced
|varA=Banana
|varB={$varTemp}
|varTemp=Split
]]
Step Rendered Code
Import inc:advanced

{$varA} {$varB} {$varC}

|varA=Banana

Banana {$varB} {$varC}

|varB={$varTemp}

Banana {$varTemp} {$varC}

|varTemp=Split

Banana Split {$varC}

This works because the includes are processed from left-to-right, top-to-bottom, and each variable is replaced in the page before the next variable is processed.
Therefore, the order in which we place our code is VERY important. For example, if we swapped the last two lines around:

Our Page:

[[include inc:advanced
|varA=Banana
|varTemp=Split
|varB={$varTemp}
]]
Step Rendered Code
Import inc:advanced

{$varA} {$varB} {$varC}

|varA=Banana

Banana {$varB} {$varC}

|varTemp=Split

Banana {$varB} {$varC}1

|varB={$varTemp}

Banana {$varTemp} {$varC}

As you can see, by changing the ordering of our code, we have changed the way our Include Variables are assigned.

Reusing Variables

We can also reuse variables in this manner:

Our Page:

[[include inc:advanced
|varA={$varTemp}
|varTemp=Go
|varB={$varTemp}
|varTemp=Bananas
]]
Step Rendered Code
Import inc:advanced

{$varA} {$varB} {$varC}

|varA={$varTemp}

{$varTemp} {$varB} {$varC}

|varTemp=Go

Go {$varB} {$varC}

|varB={$varTemp}

Go {$varTemp} {$varC}

|varTemp=Bananas

Go Bananas {$varC}

Including an Include

Through using two includes, we are able to use the value of one variable to set the value of another.

inc:advanced-2

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

Our Page:

[[include inc:advanced-2
|varA=Go
|varB=Bananas
|varTemp=varC
]]
Step Rendered Code
Import inc:advanced-2

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

|varA=Go

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

|varB=Bananas

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

|varTemp=varC

[[include inc:advanced |varA=Go |varB=Bananas |varC=**James!**]]

Step Rendered Code
Import inc:advanced

{$varA} {$varB} {$varC}

|varA=Go

Go {$varB} {$varC}

|varB=Bananas

Go Bananas {$varC}

|varC=**James!**

Go Bananas **James!**

Go Bananas James!

Creating Variables with Variables

We can even build variable names from separate parts.

inc:advanced-3:

{${$varA}{$varD}
{$varB}Apples}
{$Eat{$varC}}

Our Page:

 [[include inc:advanced-3 |varA=Go |varB={$Buy |varC=Bananas |varD=Potty} |EatBananas=Eating Bananas  ]]
Step Rendered Code
Import inc:advanced-3

{${$varA}{$varD}
{$varB}Apples}
{$Eat{$varC}}

|varA=Go

{$Go{$varD}
{$varB}Apples}
{$Eat{$varC}}

|varB={$Buy

{$Go{$varD}
{$BuyApples}
{$Eat{$varC}}

|varC=Bananas

{$Go{$varD}
{$BuyApples}
{$EatBananas}

|varD=Potty}

{$GoPotty}
{$BuyApples}
{$EatBananas}

|EatBananas=Eating Bananas

{$GoPotty}
{$BuyApples}
Eating Bananas

What Won't Work

Setting a variable doesn't change variables within the include code. Therefore, if we attempted the following code:

Our Page:

[[include inc:advanced
|varA=Go
|varB=Bananas
|varTemp=varC
|{$varTemp}=James!
]]
Step Rendered Code
Import inc:advanced

{$varA} {$varB} {$varC}

|varA=Go

Go {$varB} {$varC}

|varB=Bananas

Go Bananas {$varC}

|varTemp=varC

Go Bananas {$varC}2

|{$varTemp}=James!

Go Bananas {$varC}3

What we wanted to happen is for {$varTemp} to become varC, so that |varC=James!.
This doesn't work, because [[include]]:

Replaces Variables in the included page only, according to the the definitions in the [[include]] block

Related Articles

Authors

James KanjoJames Kanjo. Please visit his/her userPage.

rurwin does not match any existing user name. Please visit his/her userPage.

Comments

Add a New Comment

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.