Template:does-template-exist

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

Ambox content.png Warning: dangerous template
This template hacks around an important safety feature of MediaWiki. It must not be used unless it's both safe and necessary to bypass this feature. In particular, the following requirements must be met:+
  • The tested-for template must be one that is supposed to exist 100% of the time, and that does exist >99% of the time.
    • That is, this template is not a way to look for a template that may or may not exist, but a way to confirm that a template exists before calling it.
    • Otherwise, we're violating the spirit of the restriction on {{#ifexist:}}.
    • Ideally, if this template finds that the template doesn't exist, then the calling template should add the page to a cleanup category or something.
  • The tested-for template must contain only a small amount of wikitext.
    • This is because of the way this template is implemented: it actually transcludes the tested-for template and does some string-comparisons. This is unfortunate, but irremediable.
  • The calling template must be the type of template that occurs many times in a single entry.
    • If it's not, then there's no need for the hackery: we can just use {{#ifexist:Template:...}}.

This template is a total hack around the fact that MediaWiki considers #ifexist: to be an expensive parser function, and therefore limits the number of times it can be used on a page. This is somewhat reasonable — detecting whether a page exists is, in fact, much more expensive than evaluating a simple parser-function like #if: and #switch: — but on the other hand, this is something that MediaWiki does all the time, whenever a page contains a link or includes a template or belongs to a category (since MediaWiki has to distinguish redlinks from bluelinks, and also hidden categories from regular ones — let alone all the overhead of actually including a template). So, while #ifexist: should not be overused, there are a few use-cases where it really does make sense to test if a page exists, and where MediaWiki's restriction really is not reasonable. If your use-case meets the requirements in the box above, then this template is for you.

Usage

This:

{{#if:{{does-template-exist|foobar}}|yes|no}}

produces yes if {{foobar}} exists, and no if it does not.

It may also be substituted:

{{subst:#if:{{subst:does-template-exist|foobar}}|yes|no}}

See also