Extension talk:Variables

Jump to navigation Jump to search

About this board

You can use this board to submit questions and requests concerning the Variables extension. If you want to participate in development, please pick a task on Phabricator or create a new one,

Archive

Summary by MGChecker

{{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}}}} => {{#var: x }}

Ciciban (talkcontribs)

Simple question, but the article did not answer it to me: if I have defined x as {{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}}}} like here – how can I call x?

Thanks – Ciciban (talk) 06:56, 15 September 2019 (UTC)

Dinoguy1000 (talkcontribs)

{{ #var: x }}

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?"

Calling #var on a different template?

2
119.92.13.245 (talkcontribs)

I'm pretty new to this so it's still confusing to me.

So I have template1 and template 2. Template 1 does usd=12345, template 2 on the other hand has prize=5000. Is it possible to do match between these two tho they're on two different templates? I was trying the following but can't make it work and I'm stuck.

{{#vardefine:{{{result}}}|{{#expr:{{#var:prize}}*{{#var:usd}}}}}}

Dinoguy1000 (talkcontribs)

If the current template designs allow for it, I would recommend explicitly passing the needed values as template parameters, instead of relying on Variables. Not only does this bypass the problems you're facing now, but it also avoids a whole host of maintenance and code comprehension issues into the future.

Reply to "Calling #var on a different template?"

Is there a limit to the quantity of variables you have on one page?

2
Diriector Doc (talkcontribs)

I've never encountered a problem before when using many variables, but a thought came to me; How many variables can I have at once on a single page?

As an example, if I were to use, say, 100 variables one page for whatever reason, would I be able to do that without encountering a problem?

Asking out of curiosity.

Dinoguy1000 (talkcontribs)
Reply to "Is there a limit to the quantity of variables you have on one page?"

How to embed curly braces and quote-marks in variable?

7
Johnywhy (talkcontribs)

This is breaking my template. How to fix? I want the bolded part to be rendered verbatim with #var, but some characters (probably the curly braces) are getting interpreted as active wikitext characters, instead of just loaded into the variable.

{{#vardefineecho:HtmlLink|<htmltag tagname="a" href="/index.php/Portal:Tag?Tag={{{1}}}">}}

elsewhere:

{{#var:HtmlLink}}
Dinoguy1000 (talkcontribs)

The first thing I would try is surrounding the value with a <nowiki></nowiki> tag.

Johnywhy (talkcontribs)

Seems better, but still not rendering as expected.

the following:

{{#vardefineecho:HtmlLink|nowiki <htmltag tagname="a" href="/index.php/Portal:Tag?Tag={{{1}}}"> /nowiki}}

(note, the nowiki tags above ARE surrounded with < and >, but if i include them here then you cannot see the nowiki tags here)

renders, on the template page, as:

<htmltag tagname="a" href="/index.php/Portal:Tag?Tag={{{1}}}">

which is the desired wikitext.

Next, that output should then get parsed as normal wikitext in the template, where #var appears.

But, output of #var is not getting parsed as wikitext in the template, it's just getting rendered on the host-page verbatim, as if it's not wikitext.

It seems the nowiki tag in the #vardefine prevents the output of #var from getting parsed as wikitext.

Any ideas?

Dinoguy1000 (talkcontribs)

I don't think I understand what you're trying to do. What is the problem you're trying to solve here?

Johnywhy (talkcontribs)

i'm re-using a chunk of complex wikitext repeatedly in a Template. So i want to apply that wikitext to a variable for easy re-use, and easy revising.

Up to now, i tried to store the raw wikitext in the variable. But then, as reported above, the wikitext does not get parsed when retrieved with #var.

Next, i'm going to try parsing the wikitext before i store it in the variable-- if i can figure out how. Tips welcomed.

Dinoguy1000 (talkcontribs)

Is there a reason you don't use a subtemplate or metatemplate instead? It sounds like you're trying to use variables as a function of sorts, and that doesn't work; the content of a variable is parsed before it's stored in the variable.

Johnywhy (talkcontribs)

As explain in the OP above, it seems the variable can't be parsed correctly, because curlies in the value confuse your extension. That's why had to wrap it in nowiki.

Thx for telling me about subtemplate and metatemplate, i'll have to learn about that! But i get the general idea that i can use a template for re-used wikitext. Great idea.

Reply to "How to embed curly braces and quote-marks in variable?"

How to transclude invisible text?

1
Johnywhy (talkcontribs)

If template contains:

<nowiki><!-- this is a comment --></nowiki>

then the whole thing gets rendered as visible text on the host page.

If template contains:

<!-- this is a comment -->

then none of it gets transcluded.

Really just want to put some invisible text on the host page-- doesn't have to be an HTML-comment.

This doesn't get transcluded either:

<span style="display:none">Hidden Span</span>
Reply to "How to transclude invisible text?"

Distribution missing "extension.json"

2
أحمد (talkcontribs)
Kghbln (talkcontribs)

The docu states: "To users running MediaWiki 1.30 or earlier", so for MW 1.30 this extension has to be invoked via require_once ....

Reply to "Distribution missing "extension.json""

Can you query the contents of these variables for a given page id using the API?

4
174.97.28.4 (talkcontribs)
Danwe (talkcontribs)

That's not supported by the extension. You'd need to write a new extension on top of Variables to achieve this but it's not impossible.

MGChecker (talkcontribs)

It's an interesting thought to save the distinct var_final values in page_props to make it queryable… Maybe I will put some effort in implementing this additional functionality at some point in the future.

MGChecker (talkcontribs)
Reply to "Can you query the contents of these variables for a given page id using the API?"

{{#vardefineecho:n|{{#expr:{{#var:n}}+1}}}}

6
Cavila (talkcontribs)

I recently advised someone to use this in a template but I was wrong: multiple instances of such a template will print a series of 1, 1, 1, 1, etc., while use of the same syntax without a template will give you a series of 1, 2, 3, 4, etc. Is this intended behaviour?

92.21.226.2 (talkcontribs)

If you repeatedly call a template without parameters, it will reuse the output of the first call. To make sure the template is evaluated each time you need to add an empty parameter: {{template|}}.

Cavila (talkcontribs)

Great, that should do it - thanks for this!

DavidBiesack (talkcontribs)

Ouch! This makes using variables and templates much more difficult. Is there some way to bypass the cache behavior so the template is expanded every time?

Danwe (talkcontribs)

When I last worked on the extension this didn't seem possible if I remember correctly. Or it was more effort than I thought was justified to solve this.

Just call the template with an empty parameter as pointed out above.

MGChecker (talkcontribs)

This is tracked in T191574 and hopefully fixed soon.

Reply to "{{#vardefineecho:n|{{#expr:{{#var:n}}+1}}}}"
Yukii (talkcontribs)

can someone say if this extension works with MW1.30.x?

Dinoguy1000 (talkcontribs)

It does, yes.

Reply to "Works with MW 1.30.X ?"