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

would like to use #ifexpr: with #len: and #var:

8
Gunnar.offel (talkcontribs)

I would like to use a expression to check a length.

{{#ifexpr:{{#len:{{#var:refs|}} }} > 10 | [..]

That provides no exeption, as far as good, but the

{{#len:{{#var:refs|}} }}

provides always 0 so the check doesn't work. Any Idea how to solve the issue or change the behavior?

____ i wrote the ask allready in Help_talk:Extension:ParserFunctions but maybe this point is the better choice.

Gunnar.offel (talkcontribs)

i tried also the given alternatives, but sadly they also doesn't work in my case. :S

MGChecker (talkcontribs)

Are you sure the refs variable is populated? If this is not the case, you get an empty string out of `refs`, matching your reports.

MGChecker (talkcontribs)

At least

{{#vardefineecho: refs | Tree }}
{{#ifeq: {{#var: refs }} | Tree | 1 | 0 }}

works properly for me, on the contrary to the reports on the ParserFunctions. Sadly, I have no wiki with StringFunctions and Variables at hand at the moment, so I can not reproduce your claims.

Dinoguy1000 (talkcontribs)

On my own wiki with ParserFunctions, StringFunctions, and Variables, I have never had any issue using the three together as described here. @Gunnar.offel: could you provide what versions of MediaWiki, Ext:ParserFunctions, and Ext:Variables your wiki has installed?

Dinoguy1000 (talkcontribs)

Actually, based on the name of the variable, is it meant to store references? Trying to work with <ref> tags with parser functions is difficult, because of the special parsing that takes place around them. If it's an option for you, you may have better luck using Scribunto/Lua for this instead.

Gunnar.offel (talkcontribs)

@MGChecker, if i use vardefineecho and also if i call the #var it outputs the wanted things. So i'm pretty sure, that i populate the variable. i just can't get it to work with the parser function.


@Dinoguy1000: sure, - MediaWiki 1.34.0 - ParserFunctions 1.6.0 - Variables 2.5.1 (d6ce860) 08:16, 4. Sep. 2019

Yes, the variable stores the references and it works, but at the moment not in the parser functions. actually i just want to hide an empty table and only show if content is available. That's a big gun on a little problem, sure but keeps me up for a better understanding how to use/solve the extension.

MGChecker (talkcontribs)

As Dinoguy said, this is properly a really hard problem. References are a really complicated feature, kind of using variables as internal counter as well, and being evaluated at a different time during parsing. This leads to many features of stock MediaWiki already to behave weird when used together with references – especially advanced syntax features offered by extensions.

What is properly happening here that the references are not populated at the (quite early) point during parsing when variables are evaluated in this logic. I do not know exactly what is happening, but trying to get the references at multiple points, at different parsing stages, is quite typical for problems related to references.

Hence, your references var is always empty for parser functions. On the other hand, variables not encapsulated into the template tree can properly evaluated properly.

Reply to "would like to use #ifexpr: with #len: and #var:"

how to undefine a previously-defined variable?

7
2600:1700:1B0:BA0:A82F:CAEB:C7F7:8FAF (talkcontribs)

I have a template whose behavior changes based on the existence of a variable. It uses {{#varexists...}} for this purpose. I'd like to be able to define/undefine this variable multiple times, e.g.

#vardefine:debug_flag}}
{{My Template}}
{{#varundef:debug_flag}}
{{My Template}}

But currently there is no way that I know of to undefine a variable after defining it. --~~~~

Dinoguy1000 (talkcontribs)

To be honest, I've never had any use for #varexists; instead, I just define variables with a dummy value (e.g. {{ #vardefine: foo | 1 }}) and then test if the variable has a value with an #if. That way, if I need to later undefine the variable, I can simply clear its value via {{ #vardefine: foo }}.

194.230.155.242 (talkcontribs)

It is a feature that this is not possible, thus a definition of a variable can never be done.

Instead, I recommend Dinoguys approach of checking if a variable is empty. --MGC

2600:1700:1B0:BA0:988E:1F3E:9575:814C (talkcontribs)

What do you mean by "it is a feature"? What is the advantage of {{#varundef}} not existing? I guess I will have to do it the way Dinoguy1000 suggested. Equivalently, no one ever needs to use {{#varexists}} -- they can always use {{#if}}. But then, why does {{#varexists}} exist?

Dinoguy1000 (talkcontribs)

I would have to check this, but at a glance I'd guess that {{ #varexists: foo | ... }} is very slightly more efficient than {{ #if: {{ #var: foo }} | ... }} (at the very least, it does make the intention of the code clearer). I wouldn't say to avoid using #varexists entirely, but to use it only for variables that are fully static flags (i.e. the variable is defined - or not - in only one place in the template, and it isn't touched afterwards except to check for it). Certainly, now that I've taken a closer look and thought about it a bit, I'll be going over my own templates and using it where appropriate (after testing to see how it stacks up to #if: #var:, in terms of template limits, at least).

2600:1700:1B0:BA0:2D95:883D:C0D7:C5DC (talkcontribs)

I wouldn't recommend you change all of your templates for an infinitesimal efficiency improvement. You will be giving up the option of doing what I mentioned in my initial post. If you have a thousand references to your template on a page, and you only want the variable to be defined for the first half of them (maybe you're doing a binary search), you won't be able to do it.

Dinoguy1000 (talkcontribs)

I'm keenly aware of the gotcha's here, believe me. The changeover would be to benefit code readability (and thus maintainability); any potential efficiency improvement (even an infinitesimal one) would just be a nice bonus.

Reply to "how to undefine a previously-defined variable?"

Allowing more than 1000 caracters in Variables

6
2A01:CB15:258:1A00:3C42:E043:373C:AD79 (talkcontribs)

I encounter bugs when I seek to allocate a variable of more than 1000 characters. The error I get is (in french) :

<strong class="error">Erreur : la chaîne dépasse la limite maximale de 1 000 caractères.</strong>

meaning the string exceedes the maximal size of one thousand characters.

Yet I did not find any configuration parameter to allow for more than one thousand characters (I probably need 5000 or possibly more).

Any idea how I could fix this ?

Frederic.

for web site dufal.fr

e.g. you'll find the problem in page dufal.fr/index.php/Sp%C3%A9cial:Parcourir/:F.AGSO.S0001

MGChecker (talkcontribs)

This is confusing, as Variables have no such limit. Are you combining them with the Loops extension, by chance?

Can you reproduce this on a normal page, given that this case is kind of weird? I do not know how this special page comes together.

Dinoguy1000 (talkcontribs)

Are you using StringFunctions as well? These have a length limit of 1000 characters for strings. (I tried checking myself, but I don't speak French so I had a hard time telling what was going on.)

2A01:CB15:258:1A00:74BC:4610:8CC4:F7CE (talkcontribs)

Thanks for your prompt help. I'm using ParsingFunctions, Variables, and MyVariables (loaded in this order).

By adding the line

$wgPFStringLengthLimit=9996;

at the end of my LocalSettings.php rather than just after the loading of ParsingFunctions, it solved my problem. (I'm not sure I fully understand why).

Many thanks again.

Frederic.

Dinoguy1000 (talkcontribs)

That means you're using StringFunctions somewhere in the templates that page uses. The limit you increased is important for preventing denial-of-service attacks against your wiki, and the better solution here is to redesign your templates so they're not trying to pass extremely long strings through StringFunctions.

MGChecker (talkcontribs)

It would probably be better (more efficient and more versatile) to use Scribunto for string processing, instead of StringFunctions.

Reply to "Allowing more than 1000 caracters in Variables"

Works with MW 1.30.X ?

2
Summary by MGChecker

This extension works up to MW 1.35, but will possibly stop working when the new Parser is released.

Yukii (talkcontribs)

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

Dinoguy1000 (talkcontribs)

It does, yes.

Is this extension enabled on this site as well as Wikipedia?

2
Summary by Danwe

No. And it most likely never will be.

Yhynerson1 (talkcontribs)
Wargo (talkcontribs)

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

2
Summary by MGChecker

There is no limit immanent in this extension. However, the general parser limits (passed nodes etc.) still apply. You would probably have to try to make that a problem though.

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)

value starting/finished with spaces

4
Summary by MGChecker

Use <nowiki /> tags.

145.236.252.35 (talkcontribs)

I'd like to use text values on my page like this:

" any text with preceding spaces"

how can i define it?

Protnet (talkcontribs)
{{#vardefine:text|&nbsp;any text with preceding spaces}}
Loizbec (talkcontribs)

This works… unless you want to put the result of a model inside a {{#tag:syntaxhighlight}} template where the &nbsp; tag will show (which is the problem, I am having right now…)

To me, there should be a "trim" parameter to the #vardefine template :

  • {{#vardefine:text| text }}/{{#vardefine:text| text |true}}/{{#vardefine:text| text |trim}}
    → stores “text”
  • {{#vardefine:text| text |false}}
    → stores “  text  

Or if the problem comes from the way mediawiki handles template :

  • {{#vardefine:text| my text is "my text" }}
    → stores “my text is "my text"”
  • {{#vardefine:text| ""my text is my text"" }}
    → stores “"my text is my text"”
  • {{#vardefine:text| " my text is "my text" " }}
    → stores “  my text is "my text"  
  • {{#vardefine:text|" my text is "my text" "}}
    → stores “  my text is "my text"  

Or a combination of the above (parameter allowing the use of “"” to delimit text that would otherwise be trimmed).

MGChecker (talkcontribs)

You can do this by using <nowiki /> tags:

{{#vardefine:text|<nowiki /> any text with preceding spaces}}

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

8
Summary by MGChecker

This has been resolved with T191574.

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.

Gunnar.offel (talkcontribs)

So theres still no easy way to increment the variable to count anything?

MGChecker (talkcontribs)

There is. This is resolved in Variables 2.4+, which has been released in 2018.

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