Generating tabular output (examples): Difference between revisions

Add example for a (hopefully) good transition in the learning
imported>FrozenPlum
m (FrozenPlum moved page Test table to Generating tabular output (examples) without leaving a redirect)
imported>FrozenPlum
(Add example for a (hopefully) good transition in the learning)
 
(8 intermediate revisions by the same user not shown)
Line 1:
{{DPL manual|section=Examples|subsection=Generating tabular output}}
The following tests explain how to use the {{DPL|table}} parameter.
{{tocright}}
 
 
The following examples explain how to generate tabular output.
== Include 1 template value==
 
==Using template parameter values==
This selects articles in [[:Category:Fruit examples]] whose article names are automatically generated and placed in the first column (unless chosen to be omitted). It then includes the "Fruit-Infobox" template "grows" parameter value, for each article in the result set, in the second column. The <code>table=</code> statement sets the table styles (if desired) before the first comma, and then sets the header column text. The <code>tablerow=</code> statement can be used to format some or all of the output, '''but starting after''' the page name, so '''it corresponds to the second column'''so it colors the second column green; <code>%%</code> is used to position the value after the formatting. The results are limited to a count of 5.
=== Single template value===
 
 
'''Example:'''<br><br><!-- transclude example from subpage-->{{#lsth:Examples/Parameter: tablerow (example 1)|Example}}
'''Note:''' The <code>%%</code> in <code>tablerow</code> allows precise positioning of the column value, inside whatever formatting you want to apply.
{{example|tablerow|base=Controlling output format|eg=1}}
 
'''Result:'''<br><!-- transclude result from subpage-->{{#lsth:Examples/Parameter: tablerow (example 1)|Result}}
 
 
'''Example:'''<br>
'''Note:''' The <code>%%</code> in <code>tablerow</code> allows precise positioning of the column value, inside whatever formatting you want to apply.
<!-- The <code>{{DPL|tablerow}}</code> statement is best explained by an example:
<pre>
{{#<dpl:>
category=Help
|category = Fruit examples
titlematch=DPL%
|include = {Fruit-Infobox}:grows
count=4
|table = class="wikitable sortable",Page,The first parameter<br>of the Template Call
includepage ={DPL Manual}:section,%-1[25]
|tablerow = style="background:#eeffee;"¦%%
table =,Title,Sub Title,Last Section (25 chars)
|count = 5
tablerow =style="text-align:center;background-color:lightyellow;"|%%,|%%,
}}
</dpl>
</pre>
<dpl>
category=Help
titlematch=DPL%
count=4
includepage ={DPL Manual}:section,%-1[25]
table =,Title,Sub Title,Last Section (25 chars)
tablerow =style="text-align:center;background-color:lightyellow;"|%%,|%%,
</dpl>
 
The following example includes the use of {{DPL|addpagecounter}} and {{tt|%COUNT%}}, both of which were removed in MediaWiki 1.25.
 
<pre>
'''Result:'''<br>
{{#<dpl:>
category=African Union member states
|category = Fruit examples
nottitlematch=Sudan
|include = {Fruit-Infobox}:grows
addpagecounter=true
|table = class="wikitable sortable",Page,The first parameter<br>of the Template Call
includepage ={Infobox Country or territory}:area:population_estimate,%0[100]
|tablerow = style="background:#eeffee;"¦%%
format =,,\n|style="text-align:right;"|²{#ifexpr:%COUNT%>300¦<big>'''%COUNT%'''</big>¦%COUNT%}²,
|count = 5
table =,Country,Area,Population,Text,#hits
}}
tablerow =style="text-align:right;"|%%,style="text-align:right;"|%%,style="background-color:lightyellow;"|<small>%%</small>,\n|style="text-align:right;"|%%
</dpl>
</pre>
* We select some African countries, we exclude Sudan
* We acquire a piece of meta data about the articles we will find (in our case the usage counter)
* We include two named parameters from a template call, <nowiki>{{Infobox Country or territory}}</nowiki>
* We include a short text passage from the text before the first chapter
* We use the third parameter of <code>{{DPL|format}}</code> to output the usage counter (we could even highlight values above 100 here, for example)
* We highlight usage values above 300
* We define a standard wiki table with the article name in the first column (named "Country")
* We define column headers for transcluded contents and for the usage counter
* We care for right alignment of the numerical values and define a background-color for the text -->
 
 
====Omit (automatic) first1st column====
 
This selects articles in [[:Category:Fruit examples]], it includes the "grows" parameter value from "Fruit-Infobox" template. The <code>table</code> statement formats the table as "wikitable" and "sortable" and uses <code>-</code> in second position to omit the first (automatic) page column; the "grows" value now appears in the first column. With the first column left off, <code>tablerow</code> corresponds 1:1 to the include statement, so the ''first'' column is styled green using <code>%%</code> to position the value after the styling.
Line 63 ⟶ 90:
 
 
==== Move page to 2nd column ====
 
This selects articles in [[:Category:Fruit examples]], it includes the "grows" parameter value from the "Fruit-Infobox" template, and the page name (<code>%PAGE%</code> gives name ''with'' namespace, if applicable) which is linked, using <code>%TITLE%</code> (page name ''without'' namespace) as the link text. The <code>table</code> statement formats the table, then uses <code>-</code> in the second position to omit the first (automatic) page column. With the page column omitted, the <code>tablerow</code> formatting corresponds 1:1 to the include statement and the ''first'' column containing the "grows" parameter value is displayed using <code>%%</code>. The last column uses <code>%%</code> position the value for bold formatting.
Line 93 ⟶ 120:
 
 
==== Move page to 2nd column and format====
This selects articles in [[:Category:Fruit examples]], it includes the "grows" parameter value from "Fruit-Infobox" template, and the page (with namespace). The <code>table</code> statement uses <code>-</code> in second position to omit the (automatic) page column which makes the <code>tablerow</code> value corresponds 1:1 to the include statement position. <code>tablerow</code> has no formatting set for the first column, so its content is displayed as-is from the "grows" <code>include</code> statement, and in the second column the page (and title) is linked with italic formatting applied.
 
Line 119 ⟶ 146:
 
 
==Include=Multiple multiple templateparameter values===
 
Multiple template parameter values can be added, these can also be formatted in the <code>tablerow</code> statement.
Multiple template parameter values can be added, these can also be formatted in the <code>include</code> statement. This example selects articles in [[:Category:Fruit examples]] and includes the page title, the "image" parameter value, and the "grows" parameter value. The <code>table</code> statement formats the table, omits the regular (automatic) linked page name first column (so it can be manually inserted without link the <code>tablerow</code> line) and then creates the page headers. The <code>tablerow</code> line formats the plain (unlinked) title in italics, followed by "image" parameter value formatted as a [[mw:Help:Images#Rendering a single image|rendered image]] at 30 pixels wide, and the unformatted "grows" parameter value is displayed last.
 
 
'''Example:'''<br><br><!-- transclude example from subpage-->{{#lsth:Examples/Parameter: include (example 4)|Example}}
'''Example:'''<br>
{{example|include|base=Controlling output volume|eg=4}}
<pre>
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}:%TITLE%:image:grows
|table = ,-,Page Name, Image, Grows
|tablerow = <i>%%</i>,[[File:%%|30px]],%%
|count = 5
}}
</pre>
 
 
'''Result:'''<br><!-- transclude result from subpage-->{{#lsth:Examples/Parameter: include (example 4)|Result}}
'''Result:'''<br>
 
{{#dpl:
 
|category = Fruit examples
===#dplvar to move/duplicate parameters between columns===
|include = {Fruit-Infobox}:%TITLE%:image:grows
 
|table = ,-,Page Name, Image, Grows
'''Example:'''
|tablerow = <i>%%</i>,[[File:%%|30px]],%%
{{#lsth:Examples/Parameter: tablerow (example 2)|Example}}
|count = 5
{{ViewTest|Examples|Parameter: tablerow (example 2)}}
}}
 
'''Result:'''<br><!-- transclude result from subpage-->
{{#lsth:Examples/Parameter: tablerow (example 2)|Result}}
 
 
=== Include multiple (different) template's values===
 
This selects articles in [[:Category:Fruit examples]] whose article names are automatically generated and placed in the first column. It then includes the "Fruit-Infobox" template "grows" parameter value in the second column, and the "Color" template's first (numbered parameter) <code>1</code> value in the third column. The <code>table=</code> statement sets the table style and headings, the <code>tablerow=</code> statement's <code>%%</code> values correspond 1:1 with the position of the include statement, and allows each cell to be styled (or variables, parser functions, or other DPL3 modules to be used). The last column reuses its value, to both set the font color and display the parameter value text itself.
Line 164 ⟶ 186:
'''Notes:'''
* <code>%%</code> values can be reused '''in the same column'''.
* Since <code>%%</code> essentially "caries"allows the correspondingvalues 1:1of a column valuesto be positioned precisely within whatever formatting is to be applied; otherwise, it simply carries the value down from the corresponding <code>include</code> statementcolumn to applyits formattingcell, if other formatted values need to appear in columns after it. Given values correspond to the <code>include</code> position, they can't be reusedmoved '''inor ''other''used between columns''' in a {{DPL|tablerow}} statement, without the use of {{DPL|#dplvar}} to move the values between columns (though a surrogate templatetemplates can better enableachieve movingthis columnsame goal, allowing values to otherbe columnsused inanywhere within the sameoutput resultthey rowformat).
* '''Templates used multiple times''' on a page, have their values automatically grouped and listed under the same page line (the [[Apple]] page uses the "<code><nowiki>{{Color}}</nowiki></code>" template twice, so two color names appear), since these are grouped under the same page result, the color value <code>%%</code> can only be used to set the style for text color once, which is why the "green" value is red and not green.
* If you need the contents of templates reused on the same page to be displayed on separate lines, see the {{DPL|multisecseparators}} example of how this is done instead of <code>tablerow</code> (by using a combination of <code>include</code>, <code>listseparators</code>, <code>secseparators</code>, and <code>multisecseparators</code> instead.
Line 179 ⟶ 201:
 
 
== Using a surrogatepage templatesection contents==
 
===Single page section===
This example uses a surrogate (also known as a phantom) template to specify and/or format a single row of results, using [[mw:Help:Templates#Parameters|named]] <code><nowiki>{{{grows|}}}</nowiki></code> or <code><nowiki>{{{1|}}}</nowiki></code> [[mw:Help:Templates#Parameters|numbered]] parameters. See the source code of [[Template:Fruit-Infobox.templatevalues‎‎]].
 
 
See {{DPL|include}} for more information on surrogate templates.
 
 
'''Example:'''<br>
<pre>
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}.templatevalues
|table = ,Page,Grows,Image
|count = 5
}}
</pre>
 
 
'''Result:'''<br>
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}.templatevalues
|table = ,Page,Grows,Image
|count = 5
}}
 
 
== Using ''multiple'' surrogate templates ==
 
This example uses two surrogate (also known as a phantom) templates to specify and/or format a single row of results, using [[mw:Help:Templates#Parameters|named]] <code><nowiki>{{{grows|}}}</nowiki></code> or <code><nowiki>{{{1|}}}</nowiki></code> [[mw:Help:Templates#Parameters|numbered]] parameters. See the source code of [[Template:Fruit-Infobox.templatevalues‎‎]] and [[Template:Color.templatevalues‎‎]].
 
 
See {{DPL|include}} for more information on surrogate templates.
 
 
'''Example:'''<br>
<pre>
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}.templatevalues,{Color}.templatevalues
|table = ,Page,Fruit-Infobox<br><code>color=</code>,Fruit-Infobox<br><code>image=</code>,Color<br>(unnamed parameter whose value is specified <code>1</code>
|count = 5
}}
</pre>
 
 
'''Result:'''<br>
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}.templatevalues,{Color}.templatevalues
|table = ,Page,Fruit-Infobox<br><code>color=</code>,Fruit-Infobox<br><code>image=</code>,Color<br>(unnamed parameter whose value is specified <code>1</code>
|count = 5
}}
 
 
== Get 1 section's contents ==
 
This example includes the content of article's "Notes" section content, for pages in [[:Category:Fruit examples]] that have a section named "Notes" (pages that don't contain this show no content for it). The sort order is set to descending using <code>tablesortcol</code> to cause descending order sorting.
Line 267 ⟶ 235:
 
 
===Multiple page sections===
== Get 2 section's contents ==
This example includes article's "Notes" and "More" page section contents, for pages in [[:Category:Fruit examples]] that have these sections. The "More" section content is limited to 15 characters <code><nowiki>#More[15]</nowiki></code>. The <code>tablerow</code> statement has <code>%%</code> for each value that needsallow tofor beprecise carriedpositioning downof toits contents in applied formatting (if any) or simply display the itsvalue if correspondingno tableformatting rowis cellneeded. The second is section is formatted in italics, the output count also limited to 3 results.
 
 
Line 296 ⟶ 264:
 
 
===Dummy section trick===
 
If a #dummy section is added in the include statement (and an empty corresponding column header is also added), this causes unique behavior when the article results contain multiple sections of the same name. The result is that rather than one line per section being output (along with the page name repeating as was the case in [[#Get 1 section's contents|this example]]), the repeats are instead grouped together, each on a separate line, in one table row. The new lines from subsequent templates have been marked to begin in bold.
Line 326 ⟶ 294:
 
 
==Using surrogate templates==
==Combination of parameters and sections ==
===Single surrogate template===
 
'''Example:'''<br><br><!-- transclude example from subpage-->{{#lsth:Examples/Parameter: table (example 3)|Example}}
{{example|table|base=Controlling output format|eg=3}}
 
'''Result:'''<br><!-- transclude result from subpage-->{{#lsth:Examples/Parameter: table (example 3)|Result}}
 
===Multiple surrogate templates===
 
Multiple surrogates might be used only if you need to access parameter values from '''different templates''' used on the same page or set of pages; otherwise, it is generally better to use just one.
 
 
The below example selects pages in [[:Category:Fruit examples]], and the include statement uses two different surrogate (also known as phantom) templates to access values from two separate templates used on the result pages, and sets and formats the output for a single row (which is then re-applied to each row in the table). The first argument of the <code>table</code> statement sets the table styles, the next sets the name "Page" for the first column of values. ('''Remember''', the first column of <code>{{DPL|table}}</code> output is automatically generated by DPL3 unless suppressed with <code>-</code> (in place of "Page"), it also can't be formatted unless suppressed; and, if omitted, the first surrogate template would need to supply and format the first column values.) After the "Page" table header in the example, the rest of the titles are set. The first surrogate template formats the first two columns of results (the "grows" and "image" parameters belong to the "Fruit-Infobox" template used on result pages), and the second surrogate formats the last column (the "color" value belongs to the "Color" template used on result pages).
 
 
'''Notes:'''
* <code><nowiki>{{{%PAGE%}}}</nowiki></code> can be used to display the page name (with namespace) '''in any surrogate''' template (these are always passed by DPL3).
* <code><nowiki>{{{%TITLE%}}}</nowiki></code> can be used to display the page name (without namespace) '''in any surrogate''' template (these are always passed by DPL3).
* See {{DPL|include}} for more information on surrogate templates.
 
'''Example-specific:'''
* <code><nowiki>{{{grows|}}}</nowiki></code> was used to display the "Fruit-Infobox" template "grows" ([[mw:Help:Templates#Named parameters|named]]) parameter value from the result pages (if it existed, left it blank if not).
* <code><nowiki>{{{image|}}}</nowiki></code> was used to display the "Fruit-Infobox" template "image" ([[mw:Help:Templates#Named parameters|named]]) parameter value from the result pages (if it existed, left it blank if not).
* <code><nowiki>{{{1|}}}</nowiki></code> was used to display the "Color" template ([[mw:Help:Templates#Anonymous parameters|anonymous]]) parameter value from the result pages (if it existed, left it blank if not).
* Since the "Color" template was used more than once on some pages, it formats each instance more than once.
 
 
'''Example:'''<br>
<pre>
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}.templatevalues,{Color}.templatevalues
|table = class="wikitable sortable",Page,Grows,Image,Color
|count = 5
}}
</pre>
 
 
'''Result:'''<br>
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}.templatevalues,{Color}.templatevalues
|table = class="wikitable sortable",Page,Grows,Image,Color
|count = 5
}}
 
 
==Combining parameter values, page sections, surrogates==
 
'''Example:'''<br>
Line 332 ⟶ 348:
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}:grows:image,#Notes[20 more...],#More[20 more...],{Color}.templatevalues
|table = ,Page,Grows,Image name, Notes, More, Color
|tablerow = <b>%%</b>,<i>%%</i>,%%,%%,%%
|count = 2
Line 344 ⟶ 360:
{{#dpl:
|category = Fruit examples
|include = {Fruit-Infobox}:grows:image,#Notes[20 more...],#More[20 more...],{Color}.templatevalues
|table = ,Page,Grows,Image name, Notes, More, Color
|tablerow = <b>%%</b>,<i>%%</i>,%%,<i>%%</i>
|count = 2
Anonymous user