Manual:Advanced templates/pt
Esta página fala sobre as técnicas avançadas de modelo, particularmente, a utilização dos nomes variáveis de modelo, e os nomes de parâmetro nos moddelos. Os «Leitores», devem primeiro estar completamente familiarizados com as "técnicas padrão de modelo", encontradas em Ajuda:Modelo. Algumas das técnicas descrtas em baixo, poderão estar desatualizadas ou com utilização limitada; por exemplo, funções do «Analisador» e Lua, podem ser alternativas mais convenientes à utilização de algumas das técnicas de ramificação discutidas.
In general, these techniques rely on the recursive (inside-out) processing of templates. When a template is expanded (processed), it translates the given template code into string values. These are often sent directly to the web browser for display, however, they can be treated as code themselves. For instance, by enclosing the text strings in double-curly-brackets ( {{}} ), they can be reprocessed as template names or variable names; thus, producing different results as the string values are changed.
Notas:
- The term "variable" has two uses in this document:
- Variable (n.)
- refers to a type of magic word——a system-based variable that can be accessed in the same manner as templates——thus, {{NAMESPACE}} will return the name of the current namespace, depending on the page.
- Variable (adj. or adv.)
- used in a general sense to indicate that parts within the context may vary; hence, a variable template name, states that the name of the template being called, can be changed, according to the parameters or variables.
- Using "subst:" with manual recursion, gives a step–wise replacement, useful for analyzing and explaining the working of templates calling other templates. For instance, compare
{{{{tctc}} }}
(discussed below) with{{{{subst:tctc}} }}
, on a sandbox page. Although both produce the same visible result, the first will remain as the full code{{{{tctc}} }}
, while the second will replace the inner template{{tctc}}
with its value, leaving the code{{tc }}
.
Variáveis de modelos
editIn some cases, it may be useful for a template to call different templates depending on a variable or a passed parameter. This allows a type of branching that can be simpler than with ParserFunctions, though they are generally less versatile.
Exemplos:
- Using a variable to choose a template -
{{ {{NAMESPACE}} }}
- The magic word {{NAMESPACE}} returns the current namespace, like so: {{NAMESPACE}} = Manual. The outer brackets then process this result as a template in its own right - in this case Template:Help - which produces [[Special:MyLanguage/Manual:{{{1}}}|{{{1}}}]].
- Using a template to choose a template -
{{{{tctc}} }}
- the template {{Tctc }} contains the text "tc". This text is processed by the outer brackets as Template:tc which contains the word "in".
The extra spaces in the above examples are needed: without them, the pair of inner three braces is taken as those for a parameter. On the page itself it just shows as plain text: {{{{NAMESPACE}}}}.
- Using a parameter to choose a template -
{{{{{2}}}x|{{{1}}}}}
- The second parameter passed becomes part of the template name to which the first parameter is passed. In this case
{{2x|{{{1}}}}}
would produce{{{1}}}{{{1}}}
,{{3x|{{{1}}}}}
would produce{{{1}}}{{{1}}}{{{1}}}
, and etc. Template:Hist5 uses this technique repeatedly in {{hist5|X|4|7|3|2|9}} to produce the following 5 line histogram:
XXXX
XXXXXXX
XXX
XX
XXXXXXXXX
Using parser functions and templates, including those which process strings (see Help:String functions and Category:String manipulation templates), a template name can also depend in a more complicated way on parameters and/or variables.
Modelos passados como parâmetros
editTemplates can be passed as parameters to other templates. This can mean either that the template is evaluated and the result is passed as a parameter or that the template name is passed and evaluated as part of the other template.
- Passing a template result - {{3x|{{tc}}}}
- Template:3x contains {{{1}}}{{{1}}}{{{1}}}. {{3x|{{tc}}}} first evaluates {{Tc }} (which yields in), and passes that to template {{3x }}, to give ininin.
- Passing a template result recursively - {{3x|{{4x|{{tc}}}}}}
- Just as above except {{Tc }} (in) is first passed to {{4x }} and the result of that is passed to {{3x }}, to give inininininininininininin.
- Passing a template name -
{{tto|t|V}}
- Template:tto -
{{{{{1}}}|a{{{2}}}b{{{2}}}c{{{2}}}d}}
- takes the value V (passed as the second parameter) and produces aVbVcVd. This value is then passed to template:t (which was passed by name as the first parameter), producing "TaVbVcVd".
In interpreting this sequence {{tto|t|V}}
, the parser :
- sees that
{{tto|t|V}}
is formally a valid template call, with :- template name = "tto"
- parameter 1 = "t"
- parameter 2 = "V"
- evaluates the current values both of the template name and of its parameter,
- template = template:tto
- {{{1}}} := t
- {{{2}}} := V
- gets the current value of the template content
{{{{{1}}}|a{{{2}}}b{{{2}}}c{{{2}}}d}}
- (recursively) sees that
{{{{{1}}}|a{{{2}}}b{{{2}}}c{{{2}}}d}}
formally contains a valid template call, with :- template name = {{{1}}}
- parameter 1 = a{{{2}}}b{{{2}}}c{{{2}}}d
- evaluates the current values both of the template name and of its parameter, in the context where {{{1}}} = b and {{{2}}} = V, giving :
- template name = b
- parameter 1 = aVbVcVd
- gets the current value of the template content
start-{{{1}}}-end
- applies the template to its parameters, producing "TaVbVcVd".
- (recursively) sees that
- (recursively) returns the value.
Nome do parâmetro da variável
editA parameter name in a template can be the value of another parameter. This is useful if you want the behavior of a template to change based on the information that is provided to it.
Choosing parameters contextually - {{t pnd|parameter name}}
- Template:t p contains
{{{capital}}} is the capital of {{{country}}}
, with two parameters - "capital" and "country".- Template:t pnd containing "
{{t p|{{{1}}}=abc}}<noinclude>{{doc|content=This template is used as an example at [[Manual:Advanced templates]][[Category:Demo templates]]}}</noinclude>
" can be used to select which parameter is used in a particular case.Assim:
Isto... Produz isto {{t pnd|capital}} abc is the capital of {{{country}}}. {{t pnd|país}} {{{capital}}} is the capital of {{{country}}}. {{t pnd|qualquer coisa mais}} {{{capital}}} is the capital of {{{country}}}. This applies to integer parameters as well, since integer parameters can be specified using the "#=" notation. Passing the integer value N to {{T pnd }} as its parameter will make it look for the Nth unnamed parameter.
This is e.g. applied in w:Template:Reg polyhedra db, which contains a 2D array in the form {{{{{1}}}|{{{2}}}|1-1=a11|..|m-n=amn}}. The first parameter is the name of a template that provides a particular selection and presentation of a selected row of the array, e.g. w:Template:Reg polyhedron stat table, the second parameter (which is the first parameter of the latter template) specifies the row. The latter templates references element j of the row concerned by a tag of the form {{{{{{1}}}-j}}}
Whilst the same output could also be produced using {#switch:}, this method is less intensive on the server and may help to stay under page limits; see Help:Array.
- Parameter name from another parameter in the same template -
{{ppp|p=foo|foo=bar}}
A parameter name is a wikitext that is expanded before being used as name.
For instance, using Template:ppp, which contains {{{{{{p}}}}}}
(six braces, the parameter name is itself a parameter), the code {{ppp|p=foo|foo=bar}}
:
- Sets the parameter named {{{foo}}} to the value "bar", and then set the parameter named {{{p}}} to the value of foo. The order in which the parameters appear in the code does not matter.
- Then the content of template:ppp is used, the parser analyzing it as
{{{...}}}
= a variable name.- It then isolates the wikitext that takes place in this variable names, which is
{{{p}}}
. - It then expands that wikitext, finding it to be a valid variable name.
- It replaces the variable name p by its wikitext value, yielding "foo".
- It then isolates the wikitext that takes place in this variable names, which is
- After inner expansion, the variable is therefore
{{{foo}}}
- It replaces the variable name foo by its value, yielding "bar".
The result of the template call is therefore bar.
The name expansion is recursive, so the technique can be applied multiple times—e.g., using Template:tvvv, which contains {{{{{{{{{p}}}}}}}}}
, {{tvvv|p=foo|foo=bar|bar=biz}}
gives :
- Variable initialization : {{{p}}} = "foo" ; {{{foo}}} = bar ; {{{bar}}} = "biz".
- Template expansion is {{{ ... }}} with parameter name = {{{{{{p}}}}}}.
- Parameter name expansion is {{{...}}} with parameter name = {{{p}}}.
- Parameter name expansion is {{{...}}} with parameter name = "p".
- Parameter {{{p}}} has been initiated to "foo" : expansion wikitext is "foo".
- Parameter {{{foo}}} has been initiated to "bar" : expansion wikitext is "bar".
- Parameter name expansion is {{{...}}} with parameter name = {{{p}}}.
- Parameter {{{bar}}} has been initiated to "biz" : expansion wikitext is "biz".
Result is biz.
And so on, using Template:tvvvv, which contains {{{{{{{{{{{{p}}}}}}}}}}}}
(forth level indirection, twelve braces), {{tvvvv|p=alpha|alpha=beta|beta=gamma|gamma=delta}}
gives delta (whatever use that may have).
=Branching techniques without ParserFunctions =
editThe parameter default feature was introduced before Extension:ParserFunctions. This led to the development of branching methods through the parameter default mechanism.
- If-defined branches -
{{{test{{{test|}}}|{{{then}}}}}}
- If no value is passed for the parameter test, then {{{test{{{test|}}}|{{{then}}}}}} resolves to {{{test|{{{then}}}}}} and returns the value of the parameter "then" (since no value was passed for test).
- If the parameter "test" is assigned the value "boo", however, {{{test{{{test|}}}|{{{then}}}}}} resolves to {{{testboo|{{{then}}}}}}, and so long as no parameter "testboo" exists, then this will return the value of the parameter "then".
- If the parameter "test" is defined but its value is empty, {{{test|}}} is defined and its value is empty, therefore {{{test|{{{then}}}}}} is defined and its value is empty.
Consulte Template:Ifwpc ou Template:if para comparações.
There was also an array technique using parameter defaults, with the disadvantage that a template using this technique had to be called with, in addition to the normal parameters, a standard parameter definition not reflecting a choice, but necessary to make the template work.
An even older branching technique dates from before the introduction of the parameter default mechanism. It is based on the fact that if in a template call a parameter is assigned a value more than once, the last one counts. This is used in combination with specifying the value of a parameter in a template call, where the name of that parameter depends on a parameter of an outer template. In a call {{a|b=c|{{{d}}}=e}}
, template:a uses b=c if b≠{{{d}}} and b=e if b={{{d}}}.
Another old "branching technique" is using a template name depending on the value of a parameter (see above).
Nomes de variável da variável
edit- Magic word depending on a template -
{{t curr}}
- Template:t curr containing "
{{CURRENT{{{1|DAY}}} }}<noinclude>{{doc|content=This template is used as an example at [[Manual:Advanced templates]][[Category:Demo templates]]}}</noinclude>
" gives the text 21 without parameters, since it defaults to {{CURRENTDAY}}, but {{t curr|DAYNAME}} gives Thursday while {{t curr|MONTHNAME}} gives November. Any magic word that begins with "CURRENT" can be accessed this way. - Parser function parameter depending on a template parameter -
{{ns:{{{1}}}}}
- In Template:Namespace, which contains "
namespace number:{{{1}}} - name: {{ns:{{{1}}}}}<noinclude>{{doc|content=This template is used as an example at [[Manual:Advanced templates]][[Category:Demo templates]]}}</noinclude>
",{{namespace|4}}
gives "namespace number:4 - name: Project", because Meta is the name of namespace 4.
Consulte também
edit- ParserFunctions (syntax for #if:, #expr:, etc.)
- Manual:Representing arrays