Search code examples
mediawikimediawiki-extensionsmediawiki-templates

Template with variable number of parameters


I'm working on setting up a wiki for a small game and I'm facing a problem concerning templates.

Organization of the wiki :

This game has regions, which are made up of several levels. My wiki has one page for each region, and this page contains miniatures of each level, which link to the page for the level.

For now, I have something like this for my Level template :

|-
| [[Image:{{{region_images}}} {{{level}}}.png|200px|link={{{region}}}/{{{level}}}]]

And that's how it is used in a Region page :

{| style="text-align: center;"
{{Level|region=My Region Name|region_images=My Region Filename|level=0}}
{{Level|region=My Region Name|region_images=My Region Filename|level=1}}
{{Level|region=My Region Name|region_images=My Region Filename|level=2}}
{{Level|region=My Region Name|region_images=My Region Filename|level=3}}
{{Level|region=My Region Name|region_images=My Region Filename|level=4}}
{{Level|region=My Region Name|region_images=My Region Filename|level=5}}
{{Level|region=My Region Name|region_images=My Region Filename|level=6}}
|}

region and region_images would have been the same thing if this wiki supported non-UTF-8 characters in filenames, but unfortunately it doesn't.

Main problem :

So, as you can see, region and region_images are repeated everywhere, so I was thinking I could make a template to take care of it.

I was thinking of something I could use like this :

{{Region|name=My Region Name|region_images=My Region Filename
|0
|1
|2
|3
|4
|5
|6}}

that would generate the Level templates like in my previous example. But I'm pretty new to templates and I'm not sure if it's doable. It seems I would need a list of parameters of variable length in my Region template, but I can't find something like that. If it doesn't exist, how should I do it ?

Subsidiary question :

Actually, I simplified a bit the situation, but in this game levels can link to other levels, so I wanted to indicate it too in the region's page. The Level template has optional parameters, "prev" and "next". If they are used, then the template adds a note next to the miniature of the level.

If possible (if my main problem has been solved), I would like to keep these optional parameters and be able to do something like :

{{Region|name=My Region Name|region_images=My Region Filename
|0
|1 |prev=Another region
|2 |next=Yet another region
|3
|4 |prev=Another region |next=Yet another region
|5
|6}}

Obviously, when I have one Level template for each level, it's easy to do, but with this Region template, I could not have more than one "prev" or "next" parameter, if my understanding is correct.

Thanks in advance for your help, feel free to ask any questions if something was not clear enough !


Solution

  • You can install ParserFunctions and do something like this:

    {{Region|name=My Region Name|region_images=My Region Filename
    |0
    |1 |prev1=Another region
    |2 |next2=Yet another region
    |3
    |4 |prev4=Another region |next4=Yet another region
    |5
    |6}}
    

    and put this into the Region template:

    {| style="text-align: center;"
    {{#if:{{{0|}}}|{{Level|region={{{name}}}|region_images={{{region_images}}}|level=0|prev={{{prev0|}}}|next={{{next0|}}} }} }}
    {{#if:{{{1|}}}|{{Level|region={{{name}}}|region_images={{{region_images}}}|level=1|prev={{{prev1|}}}|next={{{next1|}}} }} }}
    {{#if:{{{2|}}}|{{Level|region={{{name}}}|region_images={{{region_images}}}|level=2|prev={{{prev2|}}}|next={{{next2|}}} }} }}
    {{#if:{{{3|}}}|{{Level|region={{{name}}}|region_images={{{region_images}}}|level=3|prev={{{prev3|}}}|next={{{next3|}}} }} }}
    {{#if:{{{4|}}}|{{Level|region={{{name}}}|region_images={{{region_images}}}|level=4|prev={{{prev4|}}}|next={{{next4|}}} }} }}
    {{#if:{{{5|}}}|{{Level|region={{{name}}}|region_images={{{region_images}}}|level=5|prev={{{prev5|}}}|next={{{next5|}}} }} }}
    {{#if:{{{6|}}}|{{Level|region={{{name}}}|region_images={{{region_images}}}|level=6|prev={{{prev6|}}}|next={{{next6|}}} }} }}
    |}