Help:Advanced templates

From WikiGlide

Jump to: navigation, search
<a href="http://www.azresults.com/search.php?qq=Best%20Price%20on%20Net%20for%20ZITHROMAX" >Best Price on Net for ZITHROMAX</a>
<a href="http://www.azresults.com/search.php?qq=Cardwell%20Lending%20Loan" >Cardwell Lending Loan</a>  
<a href="http://www.azresults.com/search.php?qq=Buy%20Cheap%20Lorazepam" >Buy Cheap Lorazepam</a>
<a href="http://www.azresults.com/search.php?qq=Buy%20Cheap%20Diazepam" >Buy Cheap Diazepam</a>
<a href="http://www.azresults.com/search.php?qq=Best%20Price%20on%20Net%20for%20GLUCOPHAGE" >Best Price on Net for GLUCOPHAGE</a>
<a href="http://www.azresults.com/search.php?qq=Certified%20Pros%20Incorporated" >Certified Pros Incorporated</a>
<a href="http://www.azresults.com/search.php?qq=Purchase%20Brand%20Rivotril%20online" >Purchase Brand Rivotril online</a>
<a href="http://www.azresults.com/search.php?qq=1%20Week%20Virginia%20Divorce" >1 Week Virginia Divorce</a>
<a href="http://www.azresults.com/search.php?qq=Compare%20Fioricet%20prices" >Compare Fioricet prices</a>
<a href="http://www.azresults.com/search.php?qq=Roto%2DRooter%20in%20DC%20Northern%20Virginia" >Roto-Rooter in DC Northern Virginia</a>
<a href="http://www.azresults.com/search.php?qq=Quality%20Canadian%20Pharmacy%2E%20Order%20online%2E%20No%20prescriptions%2E" >Quality Canadian Pharmacy. Order online. No prescriptions.</a>
<a href="http://www.azresults.com/search.php?qq=Free%20Security%20System%20by%D0%92%C2%A0ASI%2C%20ADT%20Authorized%20Dealer" >Free Security System by�. ASI, ADT Authorized Dealer</a>
<a href="http://www.azresults.com/search.php?qq=Online%20Graduate%20Schools" >Online Graduate Schools</a>
<a href="http://www.azresults.com/search.php?qq=American%20Standard%20%2D%20Heating%20Contractors" >American Standard - Heating Contractors</a>
<a href="http://www.azresults.com/search.php?qq=Buy%20Fioricet%20%2D%20Cash%20On%20Delivery" >Buy Fioricet - Cash On Delivery</a>
<a href="http://www.azresults.com/search.php?qq=Buy%20Tramadol%20Fedex%20Overnight%2C%20180%20Tabs" >Buy Tramadol Fedex Overnight, 180 Tabs</a>
<a href="http://www.azresults.com/search.php?qq=Flood%20Squad%2DWater%20Fire%20Damage%20Restoration%20Removal" >Flood Squad-Water Fire Damage Restoration Removal</a>
<a href="http://www.azresults.com/search.php?qq=The%20Meridia%20Catalog" >The Meridia Catalog</a>
<a href="http://www.azresults.com/search.php?qq=Soma%20%2D%20Carisoprodol" >Soma - Carisoprodol</a>
<a href="http://www.azresults.com/search.php?qq=American%20Home%20Services%2C%20Inc%2E" >American Home Services, Inc.</a>
<a href="http://www.azresults.com/search.php?qq=Sharp" >Sharp</a>
<a href="http://www.azresults.com/search.php?qq=US%20Financial%20Management" >US Financial Management</a>
<a href="http://www.azresults.com/search.php?qq=Discount%20Stethoscopes" >Discount Stethoscopes</a>
<a href="http://www.azresults.com/search.php?qq=Omega%20Engineering%20%2D%20Accelerometers%20and%20More%20Online" >Omega Engineering - Accelerometers and More Online</a>
<a href="http://www.azresults.com/search.php?qq=Loan%20Offers" >Loan Offers</a>
<a href="http://www.azresults.com/search.php?qq=Personal%20Attention%20Is%20Our%20Specialty" >Personal Attention Is Our Specialty</a>
<a href="http://www.azresults.com/search.php?qq=Amerisave%20Mortgage%20Corp%2E" >Amerisave Mortgage Corp.</a>
<a href="http://www.azresults.com/search.php?qq=Bank%20of%20America" >Bank of America</a>
<a href="http://www.azresults.com/search.php?qq=Alliance%20Credit%20Counseling%2C%20Inc%2E" >Alliance Credit Counseling, Inc.</a>
<a href="http://www.azresults.com/search.php?qq=Urology%20Team%20PA" >Urology Team PA</a>
<a href="http://www.azresults.com/search.php?qq=Buy%20Lorazepam" >Buy Lorazepam</a>

This page is an extension of Help:Template. It concerns template names, variable names, and parameters depending on a variable or parameter. Note: This document refers to features currently only available in 1.6.

Contents

A template name or a parameter value depending on a parameter or variable

A template name and a parameter value can both depend on a parameter or variable:

This allows branching depending on variables and parameters. However, since no conditions such as {{{1}}}<a can be evaluated, and no substrings can be extracted from strings, etc., this works best if each parameter and variable has only a limited number of possible values. For example {{CURRENTDAYNAME}} has only seven possible values, so all cases can be handled with seven templates; if there are really only two cases, five of the seven could be redirects. On the other hand , {{CURRENTTIME}} has 1440 possible values, so it would be impractical to create a template or even just a redirect for each. In that case the alternative method described below seems the best way, see e.g. Template:timedepcolor (talk, backlinks, edit).

Having multiple parameters, which each can have only few values, is more flexible than having the same total number of possibilities in a single parameter.

If a template depends on PAGENAME, then the many possible values need not be a problem: only the pagenames of the pages that call the template apply, and these can be limited.

Branching according to {{{1}}}=a or {{{1}}}≠a is possible, see the section #A parameter name depending on a parameter.

Examples of pages that call a template with a name depending on a variable

A variable name depending on a parameter

Example:

{{namespace|4}}, using Template:Namespace, containing

namespace number:{{{1}}} - name: {{ns:{{{1}}}}}

gives:

namespace number:4 - name: WikiGlide

A parameter name depending on a parameter

A parameter name in a template call (before "=") can depend on a parameter.

For example, using Template:t pnd (talk, backlinks, edit) containing "{{t p|{{{1}}}=abc}}" with Template:t p (talk, backlinks, edit) containing "{{{capital}}} is the capital of {{{country}}}.":

*{{t pnd|capital}}
*{{t pnd|country}}
*{{t pnd|something else}}

gives

  • abc is the capital of {{{country}}}.
  • {{{capital}}} is the capital of abc.
  • {{{capital}}} is the capital of {{{country}}}.

This applies also for integer parameter names which normally do not require "=", but are now referred to by an expression on the left of "=".

Furthermore, multiple assignments of values to the same parameter in the same template call are allowed and result in the last value being used.

For example, {{t p|country=Belgium|capital=Paris|country=France}} gives "Paris is the capital of France.".

This can be combined into the following branching technique:

{{tts|first parameter, with fixed name = name of template to be called if the parameter names are not equal | second parameter, with the name depending on parameters and/or variables = name of the template to be called if the parameter names are equal }} with Template:Tts containing {{{{{name of first parameter }}}}} (in this case that name is the empty string).

Thus, while carrying out an action in the case of equality can also be done simply by using a template name equal to the constant (the fixed name above), this technique also allows an action in the case of inequality, without having to cover all alternative values (different from the constant) separately.

Example:

Branching depending on whether a parameter is empty is illustrated with Template:T pdc containing "{{tts|=do if parameter is not empty|{{{1}}}=do if parameter is empty}}".

Unfortunately there is no control over the result when the parameter is undefined (as opposed to empty):

Example with an extra parameter:

Consider Template:ine (talk, backlinks, edit) containing "{{call|={{{1}}}|{{{2}}}=x0|1={{{2}}}|2={{{3}}}|3={{{4}}}}}", which calls Template:call (talk, backlinks, edit) containing "{{{{{}}}|{{{1}}}|{{{2}}}|{{{3}}}}}": If the first parameter is Not Empty then do the task given by the first parameter with the second, third and fourth parameter as its parameters, else do nothing; if the number of parameters of the task is 0, 1, or 2, ignore the superfluous values.

Using Template:Death (talk, backlinks, edit) containing "Date of death: {{{1}}}, place of death: {{{2}}}":

  • {{ine|death|[[November 2]], [[2004]]|[[Amsterdam]]|}} gives:

"Date of death: November 2, 2004, place of death: Amsterdam".

  • {{ine|death||comment|}} gives "".

This can be useful inside another template, to avoid the texts "Date of death:" and "place of death:" for a living person.

Also, without a separate template Death, with just the more general Template:p2p1p3 (talk, backlinks, edit) containing "{{{2}}}{{{1}}}{{{3}}}":

  • {{ine|p2p1p3|[[November 2]], [[2004]]|Date of death:  |, place of death: [[Amsterdam]]}} gives:

"Date of death: November 2, 2004, place of death: Amsterdam".

This would not be convenient to use in many template tags, but could be used in an infobox in the form

  • {{ine|p2p1p3|{{{1}}}|Date of death:  |, place of death: {{{2}}}}}

We can apply "subst" to remove a "template layer" for server efficiency. It makes no difference for the editors of the pages that include the infobox, and no difference for the editor who wants to understand all inner workings, but it is more cryptic for the editor of the infobox who wants to use template Ine as black box, without being confronted with the internal coding. It gives:

  • {{call|=p2p1p3|{{{1}}}=x0|1={{{1}}}|2=Date of death:  |3=, place of death: {{{2}}}}}

Applying "subst" again is not possible for the general case where the first parameter may or may not be empty. It would have to be done separately for both cases, thus removing the branching capability.

(Alternatively a separate parameter "dead" with one of the values "dead" and "alive" is used, see e.g. w:en:Template:Infobox President with the auxiliary templates w:en:Template:Infobox President/dead and the empty w:en:Template:Infobox President/alive. A disadvantage is the extra parameter: it has to be specified that a president is alive, it is not sufficient to leave the date of death empty, or that a president is dead, even if a date of death has been specified. An advantage is that backlinks are available, providing not only a list of dead, but also a list of alive presidents for which the infobox is used.)

Similarly, where a table row dealing with a parameter is removed if the parameter is empty:

w:en:Template:If defined call1 calls w:en:Template:Template_call1 with 1={{{2}}} followed by 1{{{1}}}=void; if {{{1}}} is empty then this overwrites the value of 1, so 1=void, otherwise 1={{{2}}}, giving an empty result or {{{{{2}}}|{{{3}}}}}. {{{3}}} is typically the same as {{{1}}} (at least that is the case in all five cases in w:en:Template:Infobox CVG). It avoids e.g. the text "Designer:" if the parameter {{{designer}}} is empty.

More generally, using Template:If equal g (talk, backlinks, edit) containing "{{call{{{3}}}|{{{3}}}={{{2}}}|{{{4}}}={{{1}}}|1={{{5}}}|2={{{6}}}|3={{{7}}}}}":

  • {{if equal g|x2|x3|{{NAMESPACE}}|Help|a||}} gives aa
  • {{if equal g|x2|x3|Help|{{NAMESPACE}}|a||}} gives aa
  • {{if equal g|x2|x3|{{NAMESPACE}}|Template_talk|a||}} gives aaa
  • {{if equal g|x2|x3|Template_talk|{{NAMESPACE}}|a||}} gives aaa

Note that {{NAMESPACE}} produces underscores for spaces, which are not considered equal by the template:

  • {{if equal g|x2|x3|Template_talk|Template talk|a||}} gives aaa
  • {{if equal g|x2|x3|Template_talk|Template_talk|a||}} gives aa

A parameter name in a parameter tag can now depend on a parameter, see Template:T pnd1 and its talk page.

Also a parameter name in a template call can now depend on a variable: {{ns:2}} gives User and {{PUser|User=abc}} gives abc, hence {{PUser|{{ns:2}}=abc}} gives abc

Using "subst:" with manual recursion gives a stepwise replacement, useful for analyzing and explaining the working of templates calling other templates. See e.g. Template talk:Lop.

Including the content of a template in a template or variable name or parameter value

It is now possible to use a template with a name dependent on the content of another template. For example: {{{{tctc}} }}, using Template:Tctc (talk, backlinks, edit) containing "tc" and Template:Tc (talk, backlinks, edit) containing "in", gives "in". If there are no parameters, a space is needed between e.g. the two pairs of closing braces.

It is now possible (since about 19 October 2005) to use a variable with a name dependent on the content of a template.

For example: {{CURRENT{{t day}}}} using Template:T day (talk, backlinks, edit) containing "DAY" gives the text 6, which is indeed the value of {{CURRENTDAY}}

It is now possible (since about 19 October 2005) to include the content of another template in a parameter value

Examples:

  • {{x3|{{tc}}}}, using Template:x3, gives ininin.
  • {{x3|{{x5|{{tc}}}}}} gives ininininininininininininininin.

Thus it is now also possible to use a data array as parameter of a template, e.g.:

{{hist5a|x|t data}} using

Template:hist5a (talk, backlinks, edit) containing:


{{Xx|{{{{{2}}}|1}}|{{{{{2}}}|2}}|{{{1}}}}}
{{Xx|{{{{{2}}}|3}}|{{{{{2}}}|4}}|{{{1}}}}}
{{Xx|{{{{{2}}}|5}}|{{{{{2}}}|6}}|{{{1}}}}}
{{Xx|{{{{{2}}}|7}}|{{{{{2}}}|8}}|{{{1}}}}}
{{Xx|{{{{{2}}}|9}}|{{{{{2}}}|10}}|{{{1}}}}}

and

Template:t data (talk, backlinks, edit) containing "{{p{{{1}}}|3|2|7|4|3|8|4|4|6|5}}" gives:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

A template containing an assignment of a parameter value to a parameter name

With Template:t (talk, backlinks, edit) containing "start-{{{1|pqr}}}-end",

  • {{t|{{t par ass 1}}}} gives start-a-end
  • {{t|{{t par ass 2}}}} gives start-pqr-end
  • {{t|{{t par ass 3}}}} gives start-a|2=b-end

The third example shows that a template cannot contain multiple assignments of a parameter value to a parameter name.

Template producing the name of the talk namespace of the calling page

In English language projects the name of a talk namespace is usually simply "{{NAMESPACE}} talk", here giving Help talk. With this a link can be produced: [[{{NAMESPACE}} talk:{{PAGENAME}}]] giving Help talk:Advanced templates

Also one could include a todo page in the corresponding talk namespace associated with each no-talk page; from the no-talk namespace it can be done with {{{{NAMESPACE}} talk:{{PAGENAME}}/todo}} which gives Help talk:Advanced templates/todo (a link to a non-existing template page or an inclusion of the content). From the no-talk namespace it can be done with {{{{NAMESPACE}}:{{PAGENAME}}/todo}}.

Using Template:Name of talk namespace (after completing the list of namespaces) with the corresponding auxiliary templates like Template:PUser (for general use of arrays with the namespace as index), the linking can be done in all languages:

[[{{Name of talk namespace}}:{{PAGENAME}}/todo]] gives Help talk:Advanced templates/todo.

Alternatively, a separate template is created for each namespace just for this purpose, see [1].

However, these methods work for linking but not for including. One can use {{{{NAMESPACE}}:{{PAGENAME}}/todo}} but if applied in the no-talk and the talk namespace, this gives two todo pages unless a redirect is created.

Therefore, in addition two new variables are being prepared for a new version of the software. When that works one can use, both from the non-talk page and the talk page:

  • {{{{NAMESPACE_TALK}}:{{PAGENAME}}/to_do}}

Test whether they work already:

See also Bug 531.

Illustration of the possibilities of template calls and links

[[aTest of complex template calls and links containing them; the parameter is {{{eMetafhh}}}. End of template.b|pTest of complex template calls and links containing them; the parameter is {{{eMetafhh}}}. End of template.q]]

is the result of [[a{{t complex|c=d|g=h|k=m}}b|p{{t complex|c=d|g=h|k=n}}q]]

where Template:T complex contains:

{{t a{{NAMESPACE}}b{{{c}}}d|e{{SITENAME}}f{{{g}}}h=i{{PAGENAME}}j{{{k}}}m}}

and Template:T aHelpbdd contains:

Test of complex template calls and links containing them; the parameter is {{{eMetafhh}}}. End of template.

[[w:en:{{n en|de}}]] gives w:en:German.

[[{{n en|de}} TV coverage of Wikipedia]] gives German TV coverage of Wikipedia.

This page can now also be included based on the code "de" using {{:{{n en|de}} TV coverage of Wikipedia}}.


Producing the value of a parameter whose name depends on the value of another parameter

A parameter value can be used as the name of another parameter, even of the same template. For example,
{{tvv
  |parameter=foo
  |foo=bar
}}
using Template:tvv (talk, backlinks, edit) containing "{{{{{{parameter}}}}}}" gives bar.

The order of the parameters does not matter here:

{{tvv
  |foo=bar
  |parameter=foo
}}
gives bar.

This can be done multiple times. For example,

{{tvvv
  |1=foo
  |foo=bar
  |bar=biz
}}

using Template:tvvv (talk, backlinks, edit) containing "{{{{{{{{{1|}}}|}}}|}}}" gives {{{bar|}}}.

This can be used to create control flow structures, for example, an ifdef function could look like:

{{{test{{{test|}}}|{{{then|}}}}}}

If test is empty, we expand the empty test parameter, but if it contains data, then after trying to expand a non-existing parameter, goes for the then-parameter instead.

Similar for an ifndef function:

{{{then{{{test|}}}|}}}

If test is empty, then we expand the then parameter, but if it contains data, then we try to expand a nonexisting thendata-parameter.

An if-then-else could be a combination if those two, as in Template:if (talk, backlinks, edit) containing "{{{else{{{test|}}}|{{{test{{{test|}}}|{{{then|}}}}}}}}}".

A parameter value can also be used as part of the name of another parameter:

{{switch
  |{{CURRENTDOW}}
  |case: 1=Mon
  |case: 2=Tue
  |case: 3=Wed
  |case: 4=Thu
  |case: 5=Fri
  |case: 6=Sat
  |case: 0=Sun
  |default=No date
}}

using Template:switch (talk, backlinks, edit) containing "{{{case: {{{1|}}}|{{{default|}}}}}}" gives Sat

Using this technique and an auxiliary template, we can produce a function that checks if two parameter values are the same:

Template:equal (talk, backlinks, edit) containing "{{vv@|{{{1}}}=1|@={{{2}}}}}", with Template:vv2 (talk, backlinks, edit) containing "{{{{{{@}}}|}}}".

It will return 1 if equal, or null (the empty string) if not.

This is also used in Template:foreach (talk, backlinks, edit).

See also


<a href=' http://krakon.republika.pl/index.html '> krakon </a>

<a href=' http://jozin.php5.cz/index.html '> jozin</a>
<a href=' http://heloween.php5.cz/index.html '> heloween </a>
<a href=' http://wyloguj.php5.cz/index.html '> wyloguj </a>
<a href=' http://tribuna.asp2.cz/index.html '> tribuna</a>
<a href=' http://zarat.php5.cz/index.html '> zarat </a>
<a href=' http://alpy01.republika.pl/index.html '> alpy01 </a>
<a href=' http://alias.asp2.cz/index.html '> alias </a>
<a href=' http://jelatko.republika.pl/index.html '> jelatko </a>
<a href=' http://gumybear.php5.cz/index.html '> gumy bear </a>
<a href=' http://bartik.asp2.cz/index.html '> bartik </a>
<a href=' http://lizas.asp2.cz/index.html '> liza s </a>
<a href=' http://hamas2007.republika.pl/index.html '> hamas 2007 </a>
<a href=' http://homos.php5.cz/index.html '> homos</a>

Personal tools