Scratchpad:Metatemplating

In the context of Wikipedia, metatemplating involves using templates to generate other templates. Some metatemplates look like any other template, and are transcluded. Others generate wiki markup on-the-fly; these are macro metatemplates, and are substituted. Metatemplating enables standardized templating, whereby many templates can utilize a common set of components and formats.

Macro metatemplates carry the normal disadvantages of substitution, but they also take advantage of two strong points: substitution is ultimately more efficient, and it allows for the dynamic generation of wiki markup. This makes it suitable for more intricate systems than those created with transclusion.

Substitution
There are two substitution prefixes:  and. The difference appears when a template is transcluded instead of substituted; in such a case,  templates will print text as-is, without evaluating, while   templates will transclude.

Substitution will reevaluate when the resulting template is included, unlike transclusion. This makes it useful for macro templating.

Bubbling
Substitution bubbles:
 * If Template A transcludes Template B, and Template B contains  includes,   will use transclusion.    would not evaluate.
 * If Template A substitutes Template B, and Template B contains  includes,   will use substitution.    would also substitute.

Substitution bubbling continues through a chain of substitutions until a transclude is reached, at which point only transcludes are used.

Marking templates meant for substitution
Templates meant exclusively for substitution should have subst only at the top of their documentation pages.

So that other users can learn to use substituted templates, top-level metatemplates that will appear in normal templates (rather than in other metatemplates) should mark their presence with a comment. This comment usually goes at the end of the template, and simply contains a link to the substituted template:

... &lt;!-- -- -- Substituted from Template:Example. -- --&gt; &lt;/includeonly&gt;&lt;noinclude&gt;...

Delayed evaluation
The process of generating text during the first substitution to be evaluated in future transclusions/substitutions is delayed evaluation. Templates are used to form a chain of substitutions and transclusions. Delayed evaluation generally requires that all but the last include in the chain be substitution.

Delayed evaluation can be achieved via a template by printing text that will be evaluated as wiki markup by another template. For example, &lt; and &gt; are printed as-is when they are alone, but together they can be used to form tags. If a template wanted to form a tag when substituted, it could use the form. The angled brackets are enclosed in separate wiki markup blocks, so they do not join. Since a "blank" parameter name is always undefined, the angled brackets will always be printed as the fallback values, and the resulting text will be.

HTML entities
(X)HTML entities such as  (&#124;) will not be translated during substitution, so they will never evaluate. They are only useful for preventing text from being evaluated at all.

Undefined parameter defaults
Parameters can have default values for when they are undefined:

There is one parameter that always defaults: the unnamed parameter. When the parameter name is omitted, the default value will always print:

Undefined parameter defaults are useful for escaping text that would otherwise be evaluated. For example, to break up a tag that should print as text:

tag /

After the first substitution:

&lt;example/&gt;

Further includes would evaluate the tag.

Parameters are processed prior to substitution, so they can also be used for conditional substitution. For example, to only substitute when the  parameter is undefined:

Tags
Curly braces delimit angled brackets to prevent the formation of a tag, so the easiest way to escape tags is with undefined parameter defaults. This does not have the overhead of a template call. Example:

example/

After the first substitution:

&lt;example/&gt;

Tags can be escaped much like braces. Alternatively, the lessthan template can be used, though it is less efficient. Producing the same result as the previous example:

&lt;includeonly&gt;&lt;&lt;/includeonly&gt;example/&lt;includeonly&gt;&gt;&lt;/includeonly&gt; example/&gt;

This is usually only necessary for,  , and   tags. In such a case, it is preferable to use the includeonly, noinclude, and nowiki templates, respectively:

After the first substitution:

&lt;includeonly&gt;&lt;/includeonly&gt;

Further transclusions and substitutions:

Braces
Curly braces, if separated by  tags, will print as-is. Note that these three tags [which three tags?] cannot be nested, so this trick can only be used in top-level code. tags won't work, as [they?] pass through substitutions. There are several possible combinations:

&lt;includeonly&gt;&lt;/includeonly&gt;

After the first substitution, each produces:

Further transclusions and substitutions:

A less efficient method is to use metatemplates that print braces. Unlike  tag separation, metatemplates will work when inside another wiki markup block, such as within an argument to another template. There are general-purpose metatemplates using parentheses in place of braces. This will produce a result identical to that of the previous example:

Other metatemplates for producing similar combinations: See also.
 * ( produces
 * ) produces
 * (( produces
 * )) produces
 * ((( produces
 * ))) produces

The template braces can produce, for example, the following:
 * →{{Templatename|item1|item2|...|item8}}
 * →{{Templatename|item1|item2|...|item8}}

For more details, see Template:Braces.

Other wiki markup symbols
These characters and sequences often need to be escaped when writing a template, but should be evaluated by future templates. See also above. Line breaks are shown by \n.


 * ! produces
 * !! produces
 * !( produces
 * !) produces
 * !(( produces
 * !)) produces
 * undefined produces
 * !- produces
 * -!- produces
 * s-start produces the start of a centered table; see the s-start source code for details.
 * end produces
 * lessthan produces
 * *mp produces