Topic on Extension talk:Variables

If I define a variable in a template and call the template, is the scope of that variable limited to the template that created it or does the scope extend to the page that called the template?

3
165.225.0.113 (talkcontribs)

Broad question: On one page, I call TemplateA and TemplateB. TemplateA defines the variable foo = bar. Can I pass foo as a paramater to TemplateB?


Narrower question: On one page, I call TemplateA multiple times and want to increment a counter every time I call the template. Is that possible?


Specific use case: Users encounter too many acronyms in our wiki. Should we require editors to spell out all acronyms, this would make pages difficult to read. The best practice in any document is to spell out the acronym the first time, place the acronym in parentheses, and then use the acronym for the remainder of the document. Example: "The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. The W3C has 467 members."

I would like to create template pages for common acronyms and direct editors to call those templates whenever they would use the acronym. The template would check whether it had been called on a page already.If so, the template would return the short form. If not, the template would return the long form. Example page source: "<nowiki>The {{W3C}} is the main international standards organization for the World Wide Web. The {{W3C}} has 467 members.</nowiki>"


I'm open to better ways of handling the specific use case. Thank you.

Kghbln (talkcontribs)

Broad question: I believe this is possible.

Narrower question: Have a look at the NumerAlpha extension for this.

I guess it is a matter of testing in the end.

MGChecker (talkcontribs)

> Narrower question: On one page, I call TemplateA multiple times and want to increment a counter every time I call the template. Is that possible?

Yes, but because of unwanted template caching issues it was not working in all cases before Variables 2.4. For your specific use case, I would use something like {{#varexists:acr-w3c|Woorld Wide Web Consortium|W3C}}{{#vardefine:acr-w3c|1}}, probably with a generic acr template in the background: {{#varexists:acr-{{{1}}}|{{{2}}}|{{{1}}}}}{{#vardefine:acr-{{{1}}}|1}}

Reply to "If I define a variable in a template and call the template, is the scope of that variable limited to the template that created it or does the scope extend to the page that called the template?"