User:Pathoschild/Help/Template special effects

From Wikipedia, the free encyclopedia
Pathoschilduserspace map ] (Template special effects)


Substitution check[edit]

There are two methods to detect substitution, both depending on the MediaWiki parser's order of operations. They are essentially the same technique, but modified to best achieve one of two goals:

  • A clean output like "This template must be substituted", but with code left over on the page after substitution.
  • Remove the check code when substituted, but corrupt output like "{{subst:#if:||This template must be substituted}}".

There's no known way to achieve both clean visible output and code removal.

Not substituted (clean visible output, but code left behind)[edit]

{{#ifeq:{{NAMESPACE}}|{{<includeonly>subst:</includeonly>NAMESPACE}}
 |Subst'd
 |Not subst'd
}}

This method uses a logical comparison between the magic namespace word, and the same with a includeonly'd modifier. When substituted, the check will compare something like "user|user" (since the <includeonly> tag is applied), while a non-substituted check will compare something like "user|{{subst:NAMESPACE}}". If the comparison returns 'not equal', the template is not substituted.

However, the code will remain on the page when the page is substituted.

Substituted (corrupt output, but no code left behind)[edit]

{{<includeonly>subst:</includeonly>#if:{{{emptyvar|}}}
 | Subst'd
 | Not subst'd
}}

This method exploits related behaviour: when substituted, the ParserFunction does not recognize empty default parameter values (ie, it will see "{{{emptyvar|}}}" when substituted rather than the usual ""). If it is not substituted, the check fails (because the condition becomes blank) and we know it's not being substituted.

Example[edit]

The following code in a template will replace the normal template output (when it is not substituted) with an error message.

{{#ifeq:{{NAMESPACE}}|{{<includeonly>subst:</includeonly>NAMESPACE}}||{{error:not substituted|AFD}}<div style="display:none;">}}

...template code...

{{#ifeq:{{NAMESPACE}}|{{<includeonly>subst:</includeonly>NAMESPACE}}||</div>}}

The CSS style will hide the box if it is not substituted, since many users may not notice the little error message above it (or not care). That aspect can easily be removed if desired. If the CSS class is used, add a closing </div> tag below the template. Otherwise, everything below the check will be hidden.