Difference between revisions of "Template:Item/doc"

From blackwiki
Jump to navigation Jump to search
imported>Petr Matas
imported>Petr Matas
Line 30: Line 30:
  
 
=== Format each item using a template ===
 
=== Format each item using a template ===
In the recipient template, use <code>{{((}}#invoke:Item|each|template=''TransformationTemplate''|''Separator''|...}}</code> to transform all items using a given template. <code>''TransformationTemplate''</code> is called for each item with the arguments that the item has been created with, plus any additional arguments passed to {{code|#invoke}} (see {{tl|Format item}} for details). The transformed items are separated with <code>''Separator''</code> (whitespace is not stripped).
+
In the recipient template, use:
 +
{{((}}#invoke:Item|each|template=''TransformationTemplate''|''Separator''|''Prefix''|''Suffix''|...}}
 +
This transforms all items using a given template. <code>''TransformationTemplate''</code> is called for each item with the arguments that the item has been created with, plus any additional arguments passed to {{code|#invoke}} (see {{tl|Format item}} for details). The transformed items are separated with <code>''Separator''</code> and surrounded with <code>''Prefix''</code> and <code>''Suffix''</code> (whitespace is not stripped from these three optional arguments).
  
The template {{tl|Pets}} from the example produces the list of pets using the following code (note the use of newline as a <code>''Separator''</code>):
+
The template {{tl|Pets}} from the example produces the list of pets using the following code (note the use of newline as a <code>''Separator''</code> and {{code|*}} as a <code>''Prefix''</code> to produce list items):
 
  {{((}}#invoke:Item|each|template=[[Template:Show pet|Show pet]]|
 
  {{((}}#invoke:Item|each|template=[[Template:Show pet|Show pet]]|
  }}
+
  |*}}
  
 
=== Gather given parameter from all items ===
 
=== Gather given parameter from all items ===

Revision as of 15:00, 14 December 2015

This template packs structured data into a compact item, which can be passed as an argument to another template. This allows to pass multiple structured items to that template, which can iterate over the items and extract data from them.

Usage

The structured data item contains all arguments (both numbered ones and named ones) passed to the {{Item}} template. For example, {{Item|Alice|Bob|best=Eve}} creates an item with two numbered components (1=Alice, 2=Bob) and one named component (best=Eve).

Multiple items will be passed to an example recipient template {{Pets}} like this:

Code Result
{{Pets|owner=Bob
| {{Item|name=Tom|animal=cat|legs=4}}
| {{Item|name=Jerry|animal=mouse|legs=4}}
| {{Item|name=Tweety|animal=bird|legs=2}}
}}
Template:Pets

Constructs available to recipient templates

Get a component of an item

See Template:Component.

Format an item using a template

See Template:Format item.

Format each item using a template

In the recipient template, use:

{{#invoke:Item|each|template=TransformationTemplate|Separator|Prefix|Suffix|...}}

This transforms all items using a given template. TransformationTemplate is called for each item with the arguments that the item has been created with, plus any additional arguments passed to #invoke (see {{Format item}} for details). The transformed items are separated with Separator and surrounded with Prefix and Suffix (whitespace is not stripped from these three optional arguments).

The template {{Pets}} from the example produces the list of pets using the following code (note the use of newline as a Separator and * as a Prefix to produce list items):

{{#invoke:Item|each|template=Show pet|
|*}}

Gather given parameter from all items

In the recipient template, use {{#invoke:Item|gather|parameter=ParameterName|template=TransformationTemplate}} to gather the given parameter from all items. TransformationTemplate will be called once and the gathered parameters (one parameter from each item) will be passed to it.

The template {{Pets}} from the example produces the total number of legs using {{#invoke:Item|gather|parameter=legs|template=Sum}}.

See also