Jump to content

Template:Guide to Unix/Conditional

From Wikibooks, open books for an open world

{{{2}}}

How to use

[edit source]

The "conditional" template is a hack implementing a type of w:conditional statement for templates. One calls the template with a conditional tag like {{Guide to Unix/Conditional|condition|yes result|no result}}. If the condition is "no", then the wiki software substitutes the conditional tag with the no result. For other conditions (such as "yes" or an arbitrary string different from "no") the wiki software substitutes the conditional tag with the yes result.

Here are some example calls:

  • {{Guide to Unix/Conditional|yes|''text A''|'''text B'''}} is text A
  • {{Guide to Unix/Conditional|no|''text A''|'''text B'''}} is text B
  • {{Guide to Unix/Conditional|xyzzy|''text A''|'''text B'''}} is text A

In the above examples, the conditional tag is useless, because the condition is already known. But the conditional tag can be placed in other templates such as Clink where the condition is not known. This makes that the template behave differently depending on its parameter. In particular, if a parameter to some template is "no" or defaults to "no", then the template can behave differently by passing that parameter in the conditional tag. The conditional tag is a hack for an feature otherwise missing.

How it works

[edit source]

The conditional template consists of a single call to "callno", but with a hack. The hack is very similar to the hack at "ine" on Wikimedia Meta.

Parameters specified twice

[edit source]

Note that "callno" simply calls the template named by the "no" parameter. Also, callno passes template arguments 1 and 2 to the other template. Think of "callno" as a level of w:indirection.

  • So {{Guide to Unix/Callno|no=stages}} is the same as {{stages}}

The hack exploits the fact that a parameter can be called twice, which sets the second value.

  • So {{Guide to Unix/Callno|no=userpage|no=stages}} is not {{userpage}} but {{stages}}

So instead of Template:Userpage we get Template:Stages. Here is the result of {{Guide to Unix/Callno|no=userpage|no=stages}}: Unix

Wikibook Development Stages
Sparse text 0% Developing text 25% Maturing text 50% Developed text 75% Comprehensive text 100%

Using callno to implement conditional

[edit source]

It might seem that "no" is a strange name for a parameter. However, "no" is also the value to parameter 1 of "conditional" that makes conditional behave differently. Look at this call again:

  • {{Guide to Unix/Conditional|no|''text A''|'''text B'''}} is text B

We can use "callno" to have "conditional" to call "Echo1st" and display text A in most cases, but to call "echo2nd" and display text B at a "no" condition. In the "callno" call, set parameter "no" to "echo1st". This calls "echo1st" in most cases. Then set parameter "{{{1}}}" to "Echo2nd". The {{{1}}} refers to the parameter 1 of "conditional", which is the condition. If the condition is "no", this replaces the call to "Echo1st" with a call to "Echo2nd". Otherwise, it sets some parameter which "callno" ignores (unless the parameter is 1 or 2, then it remains ignored because "conditional" replaces parameters by setting them after "no" and "{{{1}}}").