Include With Defaults

Posted by ErichSteinboeck on 28 May 2008 17:42, last edited by GoVegan on 09 May 2010 05:51

Tags: conditional default include

rating: +10+x

Include Variables With Default Values

The [[include]] tag can take parameters to substitute {$…} variables in the included template.

If you want to make an include template more flexible, you can add more substitution variables to it. But there's a disadvantage: more substitution variables mean that you'll have to add more parameters to each and every of your [[include ...]] statements. And worse: if you later decide to change the include template by adding even more substitution variables, you'd have to change each of your existing [[include ...]] statements.

What if you had a way to specify default values for substitution variables? That would solve the above problem.

Here's a way how to give your substitution variables default values.

Step by Step

Step 1

Create your include template as usual, adding substitution {$…} variables wherever you think they're appropriate.

Step 2

Create an include front-end. That's just another include template containing a single [[include …]] statement of the following form:

[[include include-template-name
parameter-n={$parameter-n} | parameter-n=default-value-n |

parameter-x={$parameter-x} |
… ]]

Note:
1. parameter-n are the names of substitution variables that need default values. Those default values are specified as default-value-n in the second part
2. parameter-x are the names of substitution variables that do not need a default value
3. All possible substitution {$…} variables that are defined in the include template must be specified (either with or without a default value)

Step 3

In you page source, where you want to include your include template, use

[[include include-front-end-name parameter-x=value-x | …]]

Note:
1. You do not directly include your include template, but you include your include frond-end instead
2. You only have to specify parameters without a default value
3. Specifying parameters that have been given a default value in the include front-end is now optional

Examples

Example Include Template

A sample include template called “template” might look like:

{$heading}
A description for ``{$topic}'' follows ...

Example Include Front-End

The associated front-end include called “front-end” could be:

 [[include template heading={$heading} | heading=++++ {$topic} | topic={$topic} | topic=//Your Topic//]]

This means, that both substitution variables, heading, and topic, have been given a default value:
  • topic has a default value of “//Your Topic//
  • heading has a default value of “++++ {$topic}”

Note:
1. Default values can have embedded Wikidot tags, in the case of topic it is italic formatting
2. You can give a substitution variable a default value, that is the value of another substitution variable. In the case of heading it is a level-four heading with the value of topic
3. If you refer to the value of another substitution variable, the variable you refer to must come after where you refer to it. This is why in this case “topic=//Your Topic//” comes after “heading=++++ {$topic}”

In Your Page Source

What you type … What you get…
 [[include front-end]]]

Your Topic

A description for “Your Topic” follows …

 [[include front-end topic=Internet]]]

Internet

A description for “Internet” follows …

 [[include front-end topic=* | heading=+ Asterisk]]

Asterisk

A description for “*” follows …

 [[include front-end topic=Note | heading=//Note://]]

Note:
A description for “Note” follows …

Tricky Examples

Without knowing its value, you can refer to, and extend a default value:

 [[include front-end topic=Wikidot | heading={$heading} -- better]]]

Wikidot — better

A description for “Wikidot” follows …

If you want a substitution variable to become empty, you might use “parameter=[!----]

 [[include front-end topic=Air | heading=[!----] ]]]

A description for “Air” follows …

If you want a substitution variable which, on your include template, is defined on its own line, to be displayed without the line-break, use a trailing backslash (“\”) as in the following example.

 [[include front-end topic=Compact | heading=**Compact:** \ ]]]

Compact: A description for “Compact” follows …

Author

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