Hide label if there are no parameters set in template. (Parser functions) - mediawiki

I have an infobox like this
{{Infobox
|name = {{{name}}}
|status = {{{status}}}
|-
|! style="text-align:center; color:white; font-size:1.4em; line-height:1.3em; background:#827f75" colspan="2" {{!}}Contacts
|-
{{#if:{{{Person1|}}}|
{{!}} Person1
{{!}} {{{Person1}}}
|-
{{#if:{{{Person2|}}}|
{{!}} Person2
{{!}} {{{Person2}}}
}}
The issue I have is that the label "Contacts" will be visible even if there's no parameter set for "Person1" or "Person2". This is what i've attempted.
{{#if:{{{Person1}}} or {{{Person2}}}|
{{!}} style="text-align:center; color:white; font-size:1.4em; line-height:1.3em; background:#827f75" colspan="2" {{!}}Contacts
}}
This does however not work for some reason, it always prints out the label as if the statement is always true.

if evaluates the true branch if the condition is not empty and vice versa. At the same time {{{parameter}}} evaluates to the same string ("{{{parameter}}}") if this parameter wasn't supplied. In order for it to be evaluated to empty string, you need to provide an empty default value: {{{parameter|}}}. Also, or is neither needed nor helpful here, so the resulting expression should be:
{{#if:{{{Person1|}}}{{{Person2|}}}
or
{{#if:{{{Person1|{{{Person2|}}}}}}
For more information, see:
https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions
https://www.mediawiki.org/wiki/Help:Templates
https://www.mediawiki.org/wiki/Help:Parser_functions_in_templates

Related

how to split a string based on delimiter in go html template

I am using go html templates for alerting through bosun using prometheus queries .
Below is the use case in my promql result I get instance name as instancename:9100 (example - xyz.com:9100) while setting up alerts I just need xyz.com , below is the snippet that I used using split function but that is not working . can someone please help here
result from promql would be like below
{dc="IN",env="production",instance="xyz.com:9100",job="node-exporter",service="test"}
code snippet used using go html templates
<table style="border-spacing:0; border-collapse:collapse;">
<tr>
<td><b>Host</b></td>
<td><b>Used Memory (%)</b></td>
{{ range $v := .LeftJoin .Alert.Vars.result .Alert.Vars.result}}
{{ $res := index $v 0 }}
<td>{{(split ":" $res.Group.instance)._0 }}</td>
<td>{{$res.Group.value}}</td>
{{
<td>

Default Tokens With a Values Replace It When New Values Received

currently i facing an issues which need initialize my token as any value 1st. However the token values will change when a new values received. Below is my code:
<form>
<init>
<set token="CH1_CHW_FLOW">0</set>
</init>
</form>
<search id="header">
<query>index="rtindex" Label="CH1" Order="12" |eval Value=round(Value,3) |stats latest(Value) as Value by ID2 |rename ID2 as Label | untable Label field name | xyseries field Label name</query>
<earliest>#d</earliest>
<latest>now</latest>
<preview>
<set token="FLOW_LPS">$result.CH1_CHW_FLOW_LPS$</set>
<set token="FLOW">$result.CH1_CHW_FLOW$</set>
<set token="COOLING_LOAD">$result.CH1_COOLING_LOAD$</set>
<set token="EFF">$result.CH1_EFF$</set>
</preview>
<table id="Chiller_1" border="1px solid black">
<tr><th>HEADER Details </th><th>Values</th></tr>
<tr><td>CHWF (USGPM)</td><td>$CH1_CHW_FLOW$</td></tr>
<tr><td>FLOW (LPS)</td><td>$CH1_CHW_FLOW_LPS$</td></tr>
<tr><td>COOLING LOAD</td><td>$CH1_COOLING_LOAD$</td></tr>
<tr><td>KW/Ton</td><td>$CH1_EFF$</td></tr>
</table>
However due to unknow reason i fail to initialize the value as Zero. It show me
$result.CH1_CHW_FLOW$ instead of zero. Please advise. Thank you very much.
The way to resolve it used eval:
if(isnull($result.fieldname$),0,$result.fieldname$)

Mediawiki: Displaying a List does not work with infobox template

I created a new template and want to display a list inside my newly coded infobox. To get this working I was told to change the code like in this German tutorial.
This is how my template Infobox Music currently looks like:
|-
{{#if: {{{Sänger<includeonly>|</includeonly>}}} |
{{#ifeq: {{Str left|{{{Sänger}}} }} | *
|{{!}} style="vertical-align:top;" {{!}}
'''Sänger/in'''
{{!}}
{{{Sänger}}}
|{{!}} '''Sänger/in'''
{{!}} {{{Sänger}}}
}}
}}
In order to build a new list I edited the source code in a seperate Wiki entry like this:
{{Infobox_Music
|Sänger =
* Lady Gaga
* Michael Jackson
}}
Unfortunately, when using both of these settings my list gets displayed with the first item having an * at the beginning just as usual. Here is how it looks in HTML:
<tr>
<td> <b>Sänger/in</b>
</td>
<td> * Lady Gaga
<ul><li>Michael Jackson</li></ul>
</td></tr>
Did I miss something? What does the line {{#ifeq: {{Str left|{{{Sänger}}} }} mean?
UPDATE: Here is a snippet of my previous Infobox Music template:
{{Infobox Music
|-
{{#if: {{{Sänger|}}} | {{!}} '''Sänger/in''' {{!!}} {{{Sänger}}} }}
}}
{{Str left|{{{Sänger}}} }} is the first character of the Sänger parameter (see {{Str left}}). {{#ifeq: {{Str left|{{{Sänger}}} }} | * | ... is some kind of horrible hack to use a different layout when the parameter is a list; not trying to replicate that will help you preserve your sanity.
* will only be interpreted as a list when preceded by a newline. You can prevent stripping of the leading whitespace from the template parameter by doing something like
|Sänger = <!---->
* Lady Gaga
or you can make sure that in the template {{{Sänger}}} is preceded by a newline.

Hand over parameters from a MediaWiki template to a included one

I'm using the MediaWiki extension DynamicPageList (third-party) which can be used as a template:
{{#dpl:
|category=foo
|notcategory=bar
}}
I try to use this template in one of my templates which uses more parameter e.g.:
{{myTemplate
|category=foo
|notcategory=bar
|mypara1=bla
|mypara2=lala
}}
myTemplate looks like this:
do something with mypara1
...
do something with mypara2
...
{{#dpl:
|category=foo
|notcategory=bar
}}
I know my parameters but #dpl: can use one or many parameters.
How can I separate my parameters from the #dpl: ones? And how can I just hand over the parameters which belongs to #dpl:?
Thanks,
Ray
Finally I came up with the following solution:
DPL has an additional template #dplreplace. I'm using this to parse my parameters.
Call the template:
{{myTemplate
| filter=category:foo;notcategory:bar
| mypara1=bla
| mypara2=lala
}}
In the template I replace the : by = and ; by {{!}}.
{{#dpl:
| {{#dplreplace: {{#dplreplace: {{{filter}}} | /:/ | = }} | /;/ | {{!}} }}
| ordermethod = sortkey
| suppresserrors = true
}}
NOTE: {{!}} is a template which is replaced by |.
Regards;
Ray
Maybe I'm misunderstanding your issue, but you can just pass the parameters on to DPL, just like you would to a template, or another parser function. You might want to add an empty default in most cases:
myTemplate:
do something with {{{mypara1}}}
do something with {{{mypara2}}}
{{#dpl:
|category = {{{category|}}} <!-- default to empty string -->
|notcategory = {{{notcategory|}}} <!-- default to empty string -->
}}
Call it like this:
{{myTemplate
|category=foo
|notcategory=bar
|mypara1=bla
|mypara2=lala
}}
Will work with missing parameters too:
{{myTemplate
|category=foo
|mypara1=bla
|mypara2=lala
}}

MediaWiki: Calling Template, ParserFunctions condition depending on External data values

I'm trying to display data from MySQL DB in a table in my MediaWiki page. The data are fetched using External data extension functions #get_db_data and #for_external_table, which works fine. However, I would like to do two more things:
use the fetched value as a template name: when value in DB is x, then insert Template:X (or {{X}})
use (another) fetched value in a condition using ParserFunctions extension. Specifically, a number is returned. If the number is less than 100, display it in brackets, otherwise display nothing.
Here is the code so far:
{{#get_db_data:
db=pravidla
|from=alch
|order by=nazev
|data=nazev=nazev,prima=prima,primaproc=prima_proc,sekunda=sekunda,sekundaproc=sekunda_proc,dominanta=dominanta
}}
{| class="wikitable sortable" style="border: solid 1px black"
|+ Tabulka přísad
|''Přísada''
|''Prima''
|''Sekunda''
|''Dominanta'' {{#for_external_table:<nowiki/>
{{!}}-
{{!}} {{{nazev}}}
{{!}} {{{prima}}} ({{{primaproc}}} %)
{{!}} {{{sekunda}}} ({{{sekundaproc}}} %)
{{!}} {{{dominanta}}} }}
|}
The values in prima, sekunda and dominanta variables should be replaced by a template with the same name. Neither {{{{{prima}}}}} nor {{Lcb}}{{Lcb}}{{{prima}}}{{Rcb}}{{Rcb}} (where Lcb={ and Rcb=}) do work.
The bracket with the value in the middle two cells should be displayed only in the value is less than 100 (percent), so something like {{#ifeq: {{{primaproc}}} | 100 | | ({{{primaproc}}} %) }} should be in order, but I've had no luck despite trying.
Thank you for reading this, and any insights are welcome.
I managed to resolve both issues by replacing the ExternalData extension function #for_external_table with #display_external_table. The latter passes the retrieved data as variables to a template; the same code works when placed in a separate Template:Table row page. So the solution looks like this:
Original page, displaying the table:
{{#get_db_data:
db=pravidla
|from=alch
|order by=nazev
|data=nazev=nazev,prima=prima,primaproc=prima_proc,sekunda=sekunda,sekundaproc=sekunda_proc,dominanta=dominanta
}}
{| class="wikitable sortable" style="border: solid 1px black"
|+ Tabulka přísad
|''Přísada''
|''Prima''
|''Sekunda''
|''Dominanta''
{{#display_external_table:
template=Table row
|data=nazev=nazev,prima=prima,primaproc=primaproc,sekunda=sekunda,sekundaproc=sekundaproc,dominanta=dominanta}}
|}
Newly created Template:Table row page:
{{!}}-
{{!}} {{{nazev}}}
{{!}} {{ {{{prima}}} }} {{#ifeq: {{{primaproc}}} | 100 | | ({{{primaproc}}} %) }}
{{!}} {{ {{{sekunda}}} }} {{#ifeq: {{{sekundaproc}}} | 100 | | ({{{sekundaproc}}} %) }}
{{!}} {{ {{{dominanta}}} }}
Both using the prima, sekunda and dominanta values to call a template with the same name and conditional diplay of percents in the brackets work like a charm now.