Help:Advanced templates: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
m →‎See also: rm dup
Line 338: Line 338:


{{t complex|c=d|g=h|k=m|q=r|u=v|y=z|eMetafhh=abc}}
{{t complex|c=d|g=h|k=m|q=r|u=v|y=z|eMetafhh=abc}}

{{t complex}}


==See also==
==See also==

Revision as of 11:01, 10 May 2005

Template:H:hns This page is an extension of Help:Template.

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 is almost impossible to use that variable for branching.

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

Arrays with a template for each element

A set of templates can form a kind of associative array. A 1D array has elements which are templates with names of the form array-name index, a 2D array (matrix) has elements which are templates with names of the form array-name index1 separator index2. The indexes can be any text.

For example, on Meta there are templates with names of the form "Lang_name_-_language referred to_(language used )" (in fact, the pattern is an example of a generalized form of the name pattern, with a text, here a closing parenthesis, after the second index) with codes used for the two languages; the contents of each template is the word for a language, expressed in the same or another language, e.g. Template:Lang name - de (fr) contains "Allemand", which is "German" in French. (See list; for diagonal elements such as Template:Lang name - de (de) there are, in addition, copies such as Template:Lang name - de). See also explanation page.

In the case of a 2D array separator and the possible values of index1 and index2 should be chosen such that there is no ambiguity; no separator is needed if at least one index is of fixed length, or e.g. if the first index consists of letters and the second of digits. The software does not need to parse index1 separator index2, but for convenient human parsing a separator such as a blank space may be preferred in some cases where it is not strictly needed.

Elements of a 1D array can e.g. be referred to inside a template with {{array-name {{{index}}}}} using parameter index, or similarly with a variable. Also a page can successively call each array element using {{array-name index}} with varying index.

In the case of a 2D array, a template may have a row index as parameter and produce a list based on that row of the matrix, or similarly for columns. See e.g. Template:List of Languages.

A disadvantage of having a template for each array element is that it is extremely cumbersome to copy a large array to another project, unless a bot is used, or a developer assists.

An advantage is that the absence of a data value shows up as a link to a non-existing template, allowing an individual data value to be added easily. Depending on how fast the servers are, adding multiple values may be slower, sometimes much slower, than when fewer templates have to be edited.

Another example:

A 1D array contained in a template

In the following way an array can take the form of a template (an alternative for "P" is possible, but consistency is desirable):

  • For all values of the index used, Template:Pvalue-of-index is defined, with the content {{{value-of-index }}} (see list). Where suitable, indexes of various arrays should be named the same, so that these templates can be reused.
  • An array has the form of a template with the contents
    • {{p{{{1}}}|value-1-of-index =value-of-array-element-1 |value-2-of-index =value-of-array-element-2 |..}}
  • or if index values 1,2,3,.. are used, simply
    • {{p{{{1}}}|value-of-array-element-1 |value-of-array-element-2 |..}}
  • An array element is referred to as {{template name |index }}.

Alternatively the parameter {{{1}}} is replaced by a variable, as in Template:Nsn (name space number). Since for every applicable value of the variable a template has to be created, this is mainly suitable for variables that do not take too many values, and also in the case that the template is only applied in cases where a suitable subset of values applies.

In the case of a parameter {{{1}}} there is no reason to choose the parameter names (before the "=", and in the tag in the corresponding template) differently from the corresponding index values (the template names without the "P"); even the empty string is a valid parameter name.

One has to decide whether a blank space or an underscore is used, they are not distinguished in template names, but they are in parameter names; the templates P{{NAMESPACE}}, and hence the arrays that call the, use a blank space, where applicable, see e.g. Template:PHelp talk (although {{ns:3}} gives User talk with an underscore, it is anyway not possible to have a parameter name depend on a variable, see below). Capitalization has to be consistent for parameter names as well as inside template names, as a P comes in front.

A disadvantage compared with the system mentioned above is that the absence of a data value shows up as {{{index }}}, as opposed to presenting a link to fill in the value. Referring to a defined array element without the auxiliary Template:Pindex being defined, a link to that template does show up, allowing this to be easily fixed.

Translations

One application is a template that contains translations of a particular term, where the indexes are the language codes.

Example 1:

Template:Book contains

  • {{p{{{1}}}|de=Buch|en=book|fr=livre|nl=boek|zh-hans=图书|zh-hant=圖書}}

Template:Pfr contains {{{fr}}}, etc.

{{book|fr}} gives livre.

Example 2:

Template:Nsnp (namespace number, with parameter) contains

  • {{P{{{namespace}}}|=0|Talk=1|User=2|User talk=3|Template=10|Template talk=11|Help=12|Help talk=13}}

Template:PUser contains {{{User}}}, etc.

{{nsnp|namespace=User}} gives 2.

{{nsnp|{{NAMESPACE}}}} gives 12.

Compare Template:Nsn (namespace number), which contains

  • {{#switch:{{NAMESPACE}}|=0|Talk=1|User=2|User talk=3|Template=10|Template talk=11|Help=12|Help talk=13|NL Help=116|Overleg help=117}}

{{nsn}} gives 12 (the same).

A 2D array with each row (or column) contained in a template

A collection of templates can be used as a 2D array: each row is contained in a template, and the template names consist of a common part (which can be considered the name of the 2D array) and a row index.

Thus an array element is referred to by {{2D-array-name index1 |index2 }}

Example: 2D array N

Template:Nlanguage-code contains the word for various languages, expressed in one language.

Template:N de contains

  • {{p{{{1}}}|af=Afrikaans|de=Deutsch|en=English|fr=Französisch|nl=Niederländisch}}

{{n de|fr}} gives Französisch.

The choice of what is put together in one template (a row or a column of a given matrix) can either be based on whether {{n de|fr}} or {{n fr|de}} is a more natural notation for this word, or, as has been done here, on what is more practical in filling the templates with data.

A 2D array contained in a single template

A 2D array can be contained in a single template if a composite index of the form index1 separator index2 is used.

Thus an array element is referred to by {{2D-array-name |index1 separator index2 }}

The same remarks as above apply for the separator.

Example: Template:Ln contains

  • {{#switch:{{{1}}}|de en=German|de de=Deutsch|de nl=Duits|de fr=Allemand|fr en=French|fr de=Französisch|fr nl=Frans|fr fr=Français}}<noinclude>Small example of [[Help:Localisation|one template containing a whole 2D translations array]]</noinclude>

{{ln|de fr}} gives Allemand.

Arrays of higher dimensions

An 3D array can have in each template a single element, a 1D array, a 2D array, or the whole 3D array; correspondingly an element is referred to in one of the folowing ways:

  • {{3D-array-name index1 separator1 index2 separator2 index3 }}
  • {{3D-array-name index1 separator index2 |index3 }}
  • {{3D-array-name index1 |index2 separator index3 }}
  • {{3D-array-name |index1 separator1 index2 separator2 index3 }}

An example of a 4D array is at Commons, with elements of the form Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}} ({{{lang}}}), each in a separate template, such as commons:Template:Potd/2005-06-2 (de). Most of the templates on the project are in this array (see list).

Correspondingly there is a 3D array of image names of the form Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}} such as commons:Template:Potd/2005-06-2 and a corresponding set of images of the form [[Image:{{Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}}}}. The latter can also be considered a 3D array, but with the restriction that elements can only be accessed as such from the project itself (the elements can be accessed, and from Wikimedia projects be embedded, e.g. , but the key to them in terms of the indexes can only be accessed, as the link above shows, but not automatically used).

Other ways of organizing arrays for easy access

Sections and anchors can be used, if they are systematically named. This only allows separate viewing, not inserting array elements in other texts.

Examples:

Naming conventions of array templates

It is not obvious whether a singular or plural name should be used if a single template contains several data, but they are retrieved one at the time, e.g. the template Book above could be called "Template:Translation of "book"" or "Template:Translations of "book"". Similarly Template:Name of talk namespace could also be called Template:Names of talk namespaces.

Since editing a template in general can be considered a little more advanced than just using it by putting a template tag in a page, it can be argued that the naming can best be done from the point of view of people doing the latter, i.e. singular, just like in the case of a template for each array element: {{Translation of "book"|de}} just like the alternative {{Translation of "book" - de}}.

In-page array content

Instead of having a separate template to define the content of an array, the content of one or more arrays can be put in a template (or, if the array elements are selected based on a variable, it may also be a regular page) between other text.

In this case we have

  • {{pindex |value-1-of-index =value-of-array-element-1 |value-2-of-index =value-of-array-element-2 |..}}

or if index values 1,2,3,.. are used, simply

  • {{pindex |value-of-array-element-1 |value-of-array-element-2 |..}}

where index is an expression in terms of one or more parameters or variables.

Several arrays may use the same index value, to have corresponding selections of array elements, or different ones, e.g. to allow all combinations. An example of the former follows.

Template:Creature demo contains:

A '''{{p{{{1}}}|centaur|griffin|harpy}}''' is a cross between {{p{{{1}}}|a man|a lion|a woman}} and {{p{{{1}}}|a horse|an eagle|a vulture}}.

Thus:

  • {{creature demo|1}} gives: A centaur is a cross between a man and a horse.
  • {{creature demo|2}} gives: A griffin is a cross between a lion and an eagle.
  • {{creature demo|3}} gives: A harpy is a cross between a woman and a vulture.

Optional text

Due to the blank Template:P0, in each selection an additional option is using index "0", giving no text. This allows putting a text which is optional, 1=on, 0=off.

(Note that a blank page cannot be created directly; first create a non-blank page, e.g. with just one character, then edit the page to blank it. )

Example

Template:Variable text demo 1 contains

  • With a speed of 30 m{{x{{{1}}}|etre}}/s{{x{{{1}}}|econd}} a distance of 1800 m{{x{{{1}}}|etres}} is covered in 60 s{{x{{{1}}}|econds}}.

{{Variable text demo 1|0}} gives

  • With a speed of 30 m/s a distance of 1800 m is covered in 60 s.

{{Variable text demo 1|1}} gives

  • With a speed of 30 metre/second a distance of 1800 metres is covered in 60 seconds.

Example with multiple similar lines

{{variable text demo|1}} gives the long version of a text:

  • With a speed of 30 metre/second a distance of 600 metres is covered in 20 seconds because 600 / 30 = 20.
  • With a speed of 10 metre/second a distance of 500 metres is covered in 50 seconds because 500 / 10 = 50.

while {{variable text demo|0}} gives the short version:

This construction uses the following templates:

  • Template:Variable text demo (see wikitext) - a template that produces a list, with, depending on a parameter ("comprehensiveness toggle"), either long versions of all items, or short versions; it contains all data that is not common to all lines
  • Template:Variable line demo - defines the pattern of a list item, including which parts are only in the full version and which also in the short version; it contains the text that is common to all lines

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: Meta

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 containing "{{t p|{{{1}}}=abc}}" with Template:t p 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 is 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=do if parameter names are not equal|second parameter, with the name depending on parameters and/or variables=do if parameter names are equal}} with Template:Tts containing {{{{{name of first parameter }}}}} (in this case that name is the empty string). The two parameter values, here called "do if parameter names are not equal" and "do if parameter names are equal" are the names of templates for the two alternative actions.

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:Ut. Due to

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

Example with an extra parameter:

Consider Template:Ut: If the first parameter is Not Empty then do the task given by the second parameter with the first parameter as its parameter, else do nothing.

Using Template:Ut:

  • {{ine|[[November 2]], [[2004]]|death}} gives "{{November 2, 2004|death|{{{3}}}|{{{4}}}}}".
  • {{ine||death}} gives "{{|death|{{{3}}}|{{{4}}}}}".

This can be useful inside another template, with a tag like {{ine|{{{date of death}}}|death}}, to avoid showing the text "Date of death:" for a living person, and also avoid having to put this text in the parameter for all dead people.

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 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:Ut with Template:Ut:

However:


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

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

Impossibility to include the content of a template in a template name, variable name, or parameter value

It is not possible to use a template with a name dependent on the content of another template.

For example: {{{{tctc}}}} is trying to include a template on a page, using the text in Template:Tctc to get the name of the template. However, {{{{tctc}}}} simply gives the text {{{{tctc}}}}, not the content of the template named in Tctc.

It is not possible to use a variable with a name dependent on the content of a template.

For example: {{CURRENT{{t day}}}} using Template:T day gives the text 5, not its value.

It is not possible to include the content of another template in a parameter value

For example: {{x3|{{tc}}}}, using Template:x3, gives ininin. The first pair of end braces is matched to the first pair of start braces (not to the "inner pair" of start braces, as you might expect). Thus, "{{tc" is interpreted as the parameter value.

In particular, a template name, variable name, and parameter value cannot depend on an "array value" as explained above. To allow branching based on an array element, the method of having a template for each array element can be used, provided that plain text like "Allemand" is replaced by a template call such as {{lang name|Allemand|parameter }}, where the parameter can be used to choose between e.g. just displaying the text or using it in another template name or parameter.

For example, a template like Template:Hist5 for displaying a histogram can not be applied to an array in the sense explained above, but if an array element is 38, and this value is not put in a template in plain text but in the form {{a|38|{{{code}}}}}, then depending on the value of code the number can just be displayed, or displayed as a histogram bar, etc. These actions can be defined in Template:A which calls Template:Acode.

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}} wich 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

Template:T complex contains:

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

Using {{tcw|t complex}} gives:

Test of complex template calls and links containing them; the parameter is iAdvanced templatesjmm. End of template.

See also

Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
Versions of this help page (for other languages see further)
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences
Editing
Starting a new page · Advanced editing · Editing FAQ · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URL · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · Lists · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Magic words · System message · Substitution · Array · Calculation · Transclusion
Others
Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors
Other languages: