wp:Wrapper templates
{{short description|How-to guide}}
{{Wikipedia how-to|WP:WRAPPER}}
Wrapper templates are outer templates which wrap around simpler inner templates, to greatly extend the basic functionality of the inner templates. The concept is to structure the underlying, inner templates to be used as utility tools by various outer, wrapper templates. Templates can be wrapped to existing templates with {{mfl|template wrapper|wrap}} or {{mfl|params|concat_and_call}}.
Experience has shown that, when the underlying templates have been restructured with a full set of options to be used, then numerous variations of wrapper templates have been created to extend template functionality with a broad variety of new features.
Wrapper templates can be useful for consolidation, as they allow templates to build off of each other, whereas copying and pasting templates can make them harder to maintain.
A simple example
{{t|Wikibreak}} is a template used to indicate that someone is taking a wikibreak from editing. With 10 different parameters, it can be customised in a number of ways, but the base template is rather generic ({{tq|User:Example is taking a short wikibreak and will be back on Wikipedia soon}}). To avoid needing to type out custom messages for every possibility, wrapper templates such as {{t|exams}} were created. These templates hard-code the {{para|message}} and other parameters for ease of use. Consider the following example:
{{wikibreak
| message = User:Example is taking a short wikibreak to get ready for exams and will be back on Wikipedia once the exams are over.
| image = Nuvola apps bookcase.svg
}}
If a user is a student and wants to place this message frequently, it is a lot of text to write and/or remember! However, if they use {{tlc|exams}} on their userpage, it will show the same message, because the code above is — while slightly formatted for ease of reading — what is coded into that template. Other wikibreak templates can be found at :Category:Wikibreak templates.
Wrapper template substitution
When substitution is used on a template, the original template call is replaced by its code. This is no different for a wrapper template, though there are certain things to keep in mind when creating one if substitution is expected.
=Recursive substitution=
Sometimes if a wrapper is substituted, the intention is to subst both the main wrapper template and the original it surrounds, leaving only the internal wikitext of the base template. In this case, a subst: call will need to be added. For example, Template:Don't ping inserts a message on a talk page that one is following the page and doesn't need to be pinged in replies. It calls Template:Please ping with the no
parameter specified (i.e. {{tlc|please ping|no}}). To avoid having this inner template code show up when it is substituted, the code of {{t|don't ping}} is
There are two ways of placing the subst code into a wrapper template, with little functional difference:
- Place a {{tag|noinclude|single}} in between a
subst:
call and the template, e.g.{{safesubst: please ping|no}} - Use
safesubst: in front of the template, e.g.{{safesubst: please ping|no}}
=Hiding unused parameters=
Some subst'able wrapper templates are designed as convenience templates, either to pre-fill certain types of code in the main template or otherwise reduce the amount of text needed to be entered by the user. An example of this is in WikiProject banners for task forces; for example, inserting {{tls|WP Eclipses}} on a talk page will result in
These convenience templates may offer the option for many parameters to be entered and passed to its parent template; for example there are 9 optional parameters that can be given in a {{tlc|WP Eclipses}} call that can be passed to the main template. However, it is not always desired to insert that many blank parameters into the final template, especially if many are rarely used. In such cases, any parameters to leave out of the final code can be hidden on substitution with the following steps:
- Start with the original parameter/text that needs hiding, e.g.
|class={{{class{{!}}}
- Convert the
|
and=
into their template versions ({{t|!}} and {{t|1==}} respectively); this avoids messing up the template used in the next step →{{t|!}}class{{t|1==}}{{{class{{!}}}}}
- Use {{t|ifnotempty}} to return a blank value if {{para|class}} is not used/blank → {{tlc|ifnotempty|{{{class{{!}}}}}|{{t|!}}class{{t|1==}}{{{class{{!}}}}}}}
- Add a
safesubst: before each template call (e.g. immediately after the opening {{((}} for each template): - :→
{{safesubst: ifnotempty|{{{class|}}}|{{safesubst: !}}class{{safesubst: =}}{{{class|}}}}}
When substituted, if {{para|class}} has a value it will be passed through to the base template.
Note: when hiding parameters using this method, they cannot be the first item after the base template name; the safesubst template call will interfere with the actual transclusion of the base template and the template will simply show up as code, e.g.
=Complex code=
If both the wrapper and base template code are complex, such as creating a subst'able wrapper template to convert one infobox into another infobox, there may be issues with using
{{SAFESUBST:
|
|
}}
The "transcluded" version is the original or un-subst version of the wrapper, while the "substituted" version of the code will employ the various
Wrappers around Lua modules
Many templates, such as {{tl|if empty}} or {{tl|infobox}}, are just simple wrappers around Lua modules, in this case Module:If empty and Module:Infobox. This is straightforward to implement as the frame:getParent().args
table in Lua, which is accessed from the module, can directly read the arguments passed to the wrapper template. The template can then be as simple as
Templates
- {{tl|uses infobox person}}
- {{tl|wrapper}}
- {{tl|wraps infobox}}
- {{tl|CS1 wrapper}}