Topic on Extension talk:Cite/Flow archive

Solution: Template in refs and refs in templates

3
Hippie2000 (talkcontribs)

This may be handy for others. I wanted to create a ref template to add a reference and a refs template to create the footer automatically unless there were no refs. Usage:

 per ref: {{ref|http://...|linktext}}
 footer: {{refs}}

The idea was to collect the references and set a variable refs which enables the footer. This requires Extension:ParserFunctions and Extension:Variables.

The following 2 templates - first attempt - did not work:

ref template:

<includeonly><ref>[{{{1}}} {{{2}}}]</ref>{{#vardefine:refs|1}}</includeonly>

refs template:

{{#if: {{#var:refs|}} | <span class="mw-headline"> <h2>References</h2> </span> <references /> }}

This fails because everything between the ref tags is collected until the references tag is called, and at this moment there are no more template arguments.

The following solution works - unfolded for better readability:

ref template:

<includeonly>
{{#vardefine:link{{#var:i|0}}|{{{1|}}}}}
{{#vardefine:text{{#var:i|0}}|{{{2|}}}}}
<ref>
[{{#var:link{{#var:i|0}}}} {{#var:text{{#var:i|0}}}}]
{{#vardefine:i|{{#expr:{{#var:i|0}}+1}}}}
</ref>
{{#vardefine:i|{{#expr:{{#var:i|0}}+1}}}}
{{#vardefine:refs|1}}
</includeonly>

refs template:

{{#if:{{#var:refs|}} |  
{{#vardefine:i|0}}
<span class="mw-headline"> 
<h2>References</h2> 
</span> 
<references /> 
}}

The ref template increments a variable i to define variables link0, text0, and on next call link1, text1, and sets the refs variable.
The part within the ref tags contains a second loop counter for i for the references invocation and expands the variables correctly.
The refs template just needs to reset the loop counter before the references tag is called.

This solution is template proof etc since the variables are defined at ref tag time.

Hope this is helpful for someone.

Cavila (talkcontribs)

Thanks, that sounds useful. I will give it a whirl.

Hippie2000 (talkcontribs)

I noticed one side effect of this solution. URLs containing ?name=value don't pass as anonymous args. This is not specific to my solution but a side effect of passing anonymous template args.

In this case the reference is printed non clickable. What helps is numbering the arguments then:

{{ref|1=http://...|2=linktext}}
Reply to "Solution: Template in refs and refs in templates"