User:Tvol/Guides

From UFOpaedia
Jump to: navigation, search

Template Parameter Passthrough

In cases where you want to create specializations of a template so they can be called easily and avoid code/markup duplication, but where there are defaults that should be used for the base template, you need a /Constants subpage for the base template. An example of this structure can be found by looking at any of the templates in Category:Perks (LW2) and the base template for them, {{Ability (LW2)}}.

To set default values for the base template, I created {{Ability (LW2)/Constants}}, which contains a single switch function, one for each default value. This is its sole purpose, to receive a test parameter and return what the default value should be.

Then, to set defaults, but also allow them to be overridden, the intermediary template (say {{Suppression (LW2)}} as an example) will need to contain something like this:

| b_name = {{{ b_name | {{Ability (LW2)/Constants | b_NAME }} }}}

This provides a useful function, in that it exposes the base {{Ability (LW2)}} parameter non-destructively (it won't be overridden by anything if {{Suppression (LW2)}} is called without it), and without duplication (each specialization accesses the constants subpage and does not contain the hard-coded default value). This most importantly makes it trivial to change default values for the template in future (simply edit one page, {{Ability (LW2)/Constants}}), instead of requiring the editing of several hundred templates. (Also it's best to follow programming convention and use all caps when setting these constant values to differentiate them, since MediaWiki provides absolutely no type checking of any sort.)

Wherever a base template is being used by other templates and it is necessary for it to have known default parameters (for the sake of having standardized behaviour), this "design pattern" is recommended.

Performance and Parser Limit Considerations

In any case where the template is likely to be called often, it may be better to instead create a subpage for each template that only contains that constant's value. Less convenient to edit, and the subpages should be tracked for the sake of usability and future maintenance, but once a template's defaults have been finalized, this is the least expensive option. More info here. Note that this also comes with the caveat that it must be defined and return something; if a redlink is reached, the benefits of this method go out the window.

Use Booleans and #ifexpr Over #if

The #if parser function only checks if a string passed to it (usually a parameter) is empty or not. This is fine for doing exactly that, to check if a parameter has been explicitly set, but it has drawbacks if you're trying to use it as a true/false check. It is better to use booleans for this purpose (where 0 is false and 1 or higher is true). Doing so allows for more complicated logic checks later, such as XOR, XNOR, etc. If you use just #if when what you really want is a true/false test, then you're digging yourself into a hole; don't do it.