Search code examples
mediawikimediawiki-templates

How do I use a parameter in an #if value in mediawiki?


I'm trying to create a mediawiki template:

|-
| {{{name}}}{{#if:{{{ref|}}}|<ref>{{{ref}}}</ref>|}}{{#if:{{{ndb|}}}|<ref>https://ndb.nal.usda.gov/ndb/foods/show/{{{ndb}}}</ref>|}} || {{{size|--}}} || {{{carbs|--}}} || {{{sugar|--}}} || {{{fiber|--}}} || {{{fat|--}}} || {{{protein|--}}}

Most of it works OK but if I pass a ref or a ndb parameter, the #if doesn't work properly I get

<ref>{{{ref}}}</ref>

or

<ref>https://ndb.nal.usda.gov/ndb/foods/show/{{{ndb}}}</ref>

I don't get what I would expect:

<ref>http://the.passed.value/</ref>

or

<ref>https://ndb.nal.usda.gov/ndb/foods/show/passed_value</ref>


Solution

  • #if doesn't seem to be the cause of the issue. The reason is that <ref> is not processed in the order you're expecting. You need #tag to generate a parser or extension tag at a later template processing stage:

    Template:Google source code:

    <includeonly><!--
    -->{{#if: {{{ref|}}}
       | {{#tag: ref |{{{ref}}}}}
       }}<!--
    -->{{#if: {{{q|}}}
       | {{#tag: ref |[https://google.com/search?q={{#urlencode:{{{q}}}}} Google: {{{q}}}]}}
       }}<!--
    --></includeonly>
    

    Page source code:

    {{Google
    | ref = http://google.com/search?q=foo+bar
    | q = foo bar
    }}
    
    <references/>
    

    Page output:

    [1][2]

    1. http://google.com/search?q=foo+bar
    2. Google: foo bar