Help:Template
Template
[edit]A template is a reusable piece of wikitext that can be transcluded onto other pages. Templates help standardize formatting, centralize maintenance, and provide consistent messages, infoboxes, navigational boxes, citations, and more.
Template namespace
[edit]- Templates live in the Template: namespace (e.g.,
Template:Example). - Documentation commonly resides at
Template:Name/doc. - Sandboxes and test cases can be kept at
Template:Name/sandboxandTemplate:Name/testcases.
Basic usage
[edit]- Transclusion:
{{Template name}}
- With parameters:
{{Template name|param1=value|param2=value}}
- Named parameters improve clarity, especially when many are present.
Parameters
[edit]- Positional:
Template:Name— accessed as{{{1}}},{{{2}}}. - Named:
Template:Name— accessed as{{{title}}},{{{date}}}. - Defaults:
{{{title|Untitled}}}
UsesUntitledwhentitleis not provided.
- Fallback chains:
{{{author|{{{creator|Unknown}}}}}}
Triesauthor, thencreator, elseUnknown.
Parser functions
[edit]Common parser functions used in templates:
- Conditional:
{{#if:{{{x|}}}|X provided|X missing}}
- Equality:
{{#ifeq:{{{mode|}}}|compact|Compact view|Full view}}
- Switch:
{{#switch:{{{type|}}}|A=Alpha|B=Beta|#default=Other}}
- Variables (local to page render):
{{#vardefine:FOO|bar}}{{#var:FOO}}
- Substitutions:
{{subst:CURRENTYEAR}}
Renders the current year as a fixed value at save time.
Lua modules
[edit]- Complex logic can be implemented in Lua under the Module: namespace.
- Invoke:
{{#invoke:ModuleName|functionName|arg1=value|arg2=value}}
- Use Lua for tasks like string normalization, data lookups, date handling, and performance-sensitive branching.
- Keep module code documented at
Module:Name/docwhere applicable.
Documentation
[edit]- Place usage instructions on
Template:Name/docand transclude with
.
- Include:
* Purpose and scope * Parameters with defaults and types * Examples and edge cases * Categories for maintenance and discovery
- Provide a sandbox and testcases page for safe experimentation.
Sandboxing and testing
[edit]- Sandbox: editable draft area for changes without affecting live usage.
- Testcases: a page with inputs and expected outputs to verify behavior.
- Good practice:
* Test typical and edge inputs * Verify categories and template messages * Check rendering across skins/devices
Categories and tracking
[edit]- Add categories directly in templates when needed:
[[Category:Maintenance templates]]
- Avoid placing mainspace article categories in templates that will transclude into articles unless appropriate.
- Use tracking categories for maintenance:
{{#if:{{{date|}}}||[[Category:Pages missing dates]]}}
- Hide categories in documentation using
...when relevant.
Noinclude, includeonly, onlyinclude
[edit]...— content only on the template page (not transcluded).— content only when the template is transcluded....— only the marked part is transcluded (rest ignored).- Typical pattern:
<includeonly>Banner content</includeonly><noinclude>{{Documentation}}</noinclude>
Substitution
[edit]{{subst:Template name}}writes the template’s wikitext into the page at save time.- Useful for signatures, one-time notices, or when the resulting text should not update with future template changes.
Common mistakes
[edit]- Unbalanced tags: missing
{{or braces causing parse errors. - Placing categories that leak into articles unintentionally.
- Overusing complex logic without documentation, making maintenance difficult.
- Missing defaults causing empty labels or layout breaks.
- Not escaping pipe characters
|within table cells or links when needed.
Escaping and formatting
[edit]- Pipes in links or tables can be escaped with
{{!}}if the site provides a helper. - Code and examples can be wrapped in
to preserve formatting....
- For table templates, ensure consistent cell separators and header styles.
Performance considerations
[edit]- Minimize deeply nested conditionals; prefer Lua for heavy logic.
- Reuse shared modules for repeated computations.
- Avoid excessive
#invokecalls per render if a single call can batch the work.
Design guidelines
[edit]- Make parameters explicit and self-documenting.
- Provide sensible defaults to avoid blank outputs.
- Keep styling consistent with site conventions; use minimal inline CSS.
- Separate data and presentation where feasible (module returns data; template formats it).
Versioning and deprecation
[edit]- Mark deprecated parameters in documentation and maintain backwards compatibility where possible.
- Consider a compatibility wrapper template to redirect old usage to new logic.
Examples
[edit]Simple message:
<includeonly><div class="notice">Hello {{#if:{{{name|}}}|{{{name}}}|there}}!</div></includeonly><noinclude>{{Documentation}}</noinclude>
Usage: {{Example|name=World}} → Hello World!
With categories and documentation:
<includeonly> <div class="box">This page needs citations.</div> [[Category:Maintenance messages]] </includeonly><noinclude> {{Documentation}} </noinclude>
Lua-backed:
-- Module:Greeter local p = {} function p.greet(frame)
local name = frame.args.name or "there" return "Hello " .. name .. "!"end return p
<includeonly>{{#invoke:Greeter|greet|name={{{name|}}}}}</includeonly><noinclude>{{Documentation}}</noinclude>
See also
[edit]Templates centralize formatting and logic. Document parameters, test in sandbox, and use Lua for complex tasks while keeping outputs predictable and clean.