FANDOM


 
Line 40: Line 40:
 
* The first table row column displays <code>a</code> or nothing, the second displays <code>b</code> or nothing, and the third builds the sum of <code>a</code> and <code>b</code>.
 
* The first table row column displays <code>a</code> or nothing, the second displays <code>b</code> or nothing, and the third builds the sum of <code>a</code> and <code>b</code>.
 
* <code><nowiki>{{#expr: }}</nowiki></code> is not a template, but a parser function. You can recognize parser functions by the colon instead of the pipe. Many but not all parser functions are also preceded by a hashtag. <code><nowiki>{{#expr: }}</nowiki></code> is where your formulas go.
 
* <code><nowiki>{{#expr: }}</nowiki></code> is not a template, but a parser function. You can recognize parser functions by the colon instead of the pipe. Many but not all parser functions are also preceded by a hashtag. <code><nowiki>{{#expr: }}</nowiki></code> is where your formulas go.
* <code><nowiki>{{#expr: }}</nowiki></code> treats everything after the colon not as text like templates do, but as something to compute. It needs "clean" parameters. We don't know if an editor forgets to provide one of the parameters, so just in case, we use <code>a</code> or zero (<code><nowiki>{{{a|0}}}</nowiki></code>) and <code>b</code> or zero (<code><nowiki>{{{b|0}}}</nowiki></code>), so there's always something to add. Otherwise, a nasty {{#expr: 1+}} could occur.
+
* <code><nowiki>{{#expr: }}</nowiki></code> treats everything after the colon not as text like templates do but as something to compute. It needs "clean" parameters. We don't know if an editor forgets to provide one of the parameters, so just in case, we use <code>a</code> or zero (<code><nowiki>{{{a|0}}}</nowiki></code>) and <code>b</code> or zero (<code><nowiki>{{{b|0}}}</nowiki></code>), so there's always something to add. Otherwise, a nasty {{#expr: 1+}} could occur.
 
* Wikipedia has much information about parser functions and what they can do.
 
* Wikipedia has much information about parser functions and what they can do.
 
** General: https://meta.wikimedia.org/wiki/Help:Parser_function
 
** General: https://meta.wikimedia.org/wiki/Help:Parser_function

Latest revision as of 04:31, January 8, 2020

Yes, that's possible.

Tables as such cannot calculate, templates can. The trick is to split the table into

  • a table header that only provides the header row and a closing bracket at the end
  • a table row that takes the values and calculates with them.

{{Table header stats a8}} and {{Table row stats a8}} work this way, but they are perhaps a bad example because they are full of parameters for backwards compatibility.

The simplest way would be:

Header template {{header}}
{|
! a
! b
! sum
{{{1|}}}
|}
Row template {{row}}
| {{{a|}}}
| {{{b|}}}
| {{#expr: {{{a|0}}} + {{{b|0}}} }}
|-

To display the table on a page, the code would be

{{header
  | {{row | a=2 | b=3}}
}}
  • Note that templates use two curly brackets, parameters use three.
  • The parameter {{{1|}}} in the header means: It is the first unnamed parameter, hence the number. The pipe | means "if omitted, then display". Without pipe, the text "{{{1}}}" would be displayed if an editor omitted the parameter, which is mostly unwanted. The pipe says: If the first parameter is omitted, then display nothing.
  • In our example, we send the table row as parameter to the header which constructs the table.
  • a and b are named parameters. Normally it is more useful to use speaking parameters names. I chose a and b for simplicity.
  • The first table row column displays a or nothing, the second displays b or nothing, and the third builds the sum of a and b.
  • {{#expr: }} is not a template, but a parser function. You can recognize parser functions by the colon instead of the pipe. Many but not all parser functions are also preceded by a hashtag. {{#expr: }} is where your formulas go.
  • {{#expr: }} treats everything after the colon not as text like templates do but as something to compute. It needs "clean" parameters. We don't know if an editor forgets to provide one of the parameters, so just in case, we use a or zero ({{{a|0}}}) and b or zero ({{{b|0}}}), so there's always something to add. Otherwise, a nasty Expression error: Missing operand for +. could occur.
  • Wikipedia has much information about parser functions and what they can do.

Good luck with your project and happy coding!

Community content is available under CC-BY-SA unless otherwise noted.