Jump to content

Wikipedia:Advanced template coding

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wikid77 (talk | contribs) at 13:01, 14 March 2010 (created, as Wikipedia essay, to explain coding and debugging of complex templates). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

This essay, Wikipedia:Advanced template coding describes some techniques to improve the display or editing of templates in Wikipedia. Tactics are explained for debugging template parameters in the MediaWiki markup language. Most of the tips involve use of standard text-editors. While some special software packages exist, to allow customized editing, they are typically not available when travelling to other computers for wiki-editing.

Some techniques here are beyond the basics described in the Wikipedia help-page "Help:Template" which explains almost all basic options of template coding, also showing examples of each.

Nesting levels limited to 40

Inside a single template, the nesting limit is 40 nested expressions, such as 40 multiple "if-then-else-if...". At the 41st nested "if" keyword, then an error message might appear as: "Exceeded nesting limit". However, when not nested beyond 40 levels, then a template can contain hundreds of if-expressions and switch-branches.

Some templates have contained complex conditional calculations nested over 19 levels deep, for years. Also, some templates have contained hundreds of if-expressions, for years, just NOT all nested as one, giant: if-then-else-else-else-else-else....

Debugging

Many coding errors can be debugged, more easily, by attempting to isolate the code section where coding errors most likely occurred. Intense proofreading, of the logic flow, is most often the quickest fix, such as checking for typical syntax errors (see below: Common coding errors). However, a section of troublesome code could be copied into a short test page, then edit-preview tested there, separately. Similarly, a template could be developed, in the early stages, as multiple sections of code, each to be debugged separately, then eventually joined together, such as nested sections with if-then-else-if.

Testing, of each section of code, is crucial. There are some age-old adages to heed:

  • If it hasn't been tested, then it doesn't work.
  • You can expect what you inspect. (W. Edwards Deming)

Perhaps put a variety of examples on each template's doc subpage, to help detect problems early in development. However, for complex templates, the talk-page should contain a section of numerous examples (lots of them) to demonstrate the full scope of template features.

Defaulting parameters in expressions and if-logic

When developing sections of markup that use template parameters, try to always set each parameter with a default value, especially inside expressions or if-logic coding:

  • {{#expr: 109.75 / {{{1|1}}} }} → default {1} as 1 not zero.
  • {{#ifeq: {{{anwser|y}}}|y|show yes}}

If a particular parameter is given the same default value across the whole page, then that value could be easily changed, in a text editor, by a global search-and-replace string substitution, to change the default value to some other value, for testing each case.

If those parameters are not given default values, then those sections of code cannot be tested, during edit-preview, while editing the template. Any parameter without a default value will become the literal triple-brace text (such as the literal 7 characters: {{{x}}}), and non-defaulted parameters cannot be evaluated, in expressions or if-logic, during an edit-preview of the template page.

Common coding errors

There are several common coding errors which will cause problems when processing templates. The following can be used as a checklist, to help debug problems, when a template seems to be acting bizarre:

  • Too few closing braces: A common problem is to put only 2 closing-braces around a parameter number or name, such as "{{{1}}". Having only 2 closing-braces "}}" might treat the parameter as a template named "Template:1" (preceded by a lone "{" brace). Many people with dyslexia cannot easily count the open/close braces.
  • Unopened comments: Forgetting to insert "<!--" at the start of an HTML comment can cause bizarre results, with no error message. Forgetting the exclamation point is very common: "<--" should be "<!--".
  • Unclosed comments: Forgetting to insert "-->" at the end of an HTML comment can cause bizarre results, with no error message.
  • Omitting colon or "#" on "#ifexpr": Forgetting to insert "#" or colon for "#ifexpr:" or "#expr:" can produce bizarre results, when passed into other subtemplates.
Omitting colon becomes literal text: {{#ifexpr y=0|then zero|else not}}

Note that those common coding errors could have been easily spotted by a simple syntax checker, such as warning that 3&2 braces could be trouble: "{{{size|180px}}" is treated as "{Template:Size" trying to pass 180px as the parameter because of only 2 end-braces.

Again, always checking first for common errors, as the first step, can avoid time hunting for "complex errors" which never really existed. Remember: the MediaWiki markup language is extremely error-prone, so that's why so many coding errors occur, and that's why:

Consider the above as a checklist to try first, as a type of sanity-check for the template.

Many hideous problems truly are merely 1-minute syntax fixes.

See also

 

Notes

[ This essay is a draft to be expanded later. ]