Ricardo
Ricardo

Reputation: 1275

Connectives in mediawiki's {{#if: ... }}

I'm creating a template in mediawiki that would optionally include a piece of information only if that information is available. The information is obtained using an semantic mediawiki annotation in the article passed as template parameter. The problem is that combining multiple queries in one {{#if: ...}} with ANDs and ORs is quite cumbersome and error-prone. The way I'm doing it at the moment looks like

{{#ifexist: {{{1}}} | {{#if: {{#if: {{#show: {{{1}}} | ?prop1 }} |
  {{#if: {{#show: {{{1}}} | ?prop2 }} | {{#show {{{1}}} | ?prop3 }} }}
  {{#if: ... }} ... | ... }}

So the second and third #ifs are basically encoding an AND while the second and fourth are enconding an OR, that is, it is something like if ((prop1 in {{{1}}}) and (prop2 in {{{1}}}) and (prop3 in {{{1}}})) or .... The #ifexist is necessary because #show returns an error (that is not an empty string) when {{{1}}} doesn't exist as an article in the wiki. I have about 6 ORs and 18 ANDs, so you can imagine how long and difficult to read it becomes. I was wondering if there's a more direct way of expressing these connectives, specially AND that requires this ugly encoding with nested #ifs.

If you're wondering where do I need such a long if, the actual template that I'm working on is here: https://psychonautwiki.org/wiki/Template:Summary

Upvotes: 1

Views: 491

Answers (1)

Tgr
Tgr

Reputation: 28160

MediaWiki templates are not a programming language; if that's a problem for you, you are probably doing it wrong. Try an actual programming language instead.

If hard-pressed, I would use {{#ifexpr}}:

{{#ifexpr:
  {{#ifexist: {{{1}}} | 1 | 0 }}
  and (
    {{#show: {{{1}}} | ?prop1 | 1 | 0 }}
    and {{#show: {{{1}}} | ?prop2 | 1 | 0 }}
    or
    {{#show: {{{1}}} | ?prop3 | 1 | 0 }}
    ...
  )
|...}}

If you need to choose between multiple outputs depending on the conditions, {{#switch}} and the old switch(true) trick can also be handy:

{{#switch:1
| {{#show: {{{1}}} | ?prop1 | 1 | 0 }} = option 1
| {{#show: {{{1}}} | ?prop2 | 1 | 0 }} = option 2
...
}}

Upvotes: 3

Related Questions