Search code examples
parameter-passingmediawikiwiki-markup

In MediaWiki, can I transclude a page's contents to be used as parameters for a template?


I have a template that takes parameters called "name" and "type".

I am currently attempting to call that template and pass it the transcluded contents of a page called Input1 that simply says:

name=Thing|type=Whatsit

I am calling the template this way: {{TemplateName|{{:Input1}}}}

However the template is simply receiving the text "name=Thing|type=Whatsit". It is not parsing the text as parameters, as if I had invoked it this way:

{{TemplateName|name=Thing|type=Whatsit}}

Is there any way to coax MediaWiki to see the page's contents as actual parameters, setting {{{name}}} and {{{type}}} on that basis? I had big plans for being able to use another template, Foreach, to create many calls to a template this way, passing it Input1, Input2, etc.


Solution

  • It is not possible to transclude the contents of a page to be used as parameters to a template. MediaWiki first parses the structure of the template expression, i.e. the '{{', '|' and '}}'. Subsequently it expands the templates inside that expression, but if this expansion contains a '|' it is interpreted as a literal '|' and not a parameter separator. Hence the number of parameters can no longer change. This behavior used to be necessary to make the {{!}} template work, which was used to insert a literal '|' in a template parameter.

    However, by changing the order of the transclusions, you can still do exactly what you want. Template parameters can be used to construct the name of another template. So, you can pass the name of the template as a parameter to the page that contains your parameters: {{:Input1|TemplateName}}. Your 'Input' page would then be written as:

    {{{{{1|Standard}}}
    |Name=Input1
    |Param1=Value1
    |Param2=Value2
    }}
    

    So transcluding the page containing the parameters as {{:Input1|TableView}} would yield the desired result:

    {{TableView
    |Name=Input1
    |Param1=Value1
    |Param2=Value2
    }}