ထႅမ်းပလဵတ်ႉ:Px
This is the {{Px}} meta-template.
It helps other templates, such as infoboxes, to take image size parameters in both of the forms "40" or "40px". It takes up to three parameters and returns the first one that has any content. That is, the first one that is defined (not null) and not empty. Thus a default value for the width of the image can be fed as the second or third parameter.
Usage
[မႄးထတ်း]Here is what this template does. Code to the left, actual rendering to the right:
And with an optional second or even third parameter to be used as a default value if the first or second parameter is empty or undefined:
And here is how it can be used inside another template:
[[ၾၢႆႇ:{{{image}}} | {{Px|{{{size|}}}}} | {{{caption|}}} ]]
[[ၾၢႆႇ:{{{image}}} | {{Px|{{{size|}}}|250}} | {{{caption|}}} ]]
[[ၾၢႆႇ:{{{image}}} | {{Px| {{{size|}}} | {{{width|}}} | 250 }} | {{{caption|}}} ]]
Note! The parameters to {{Px}} must use the pipe "|", like this: {{{width|}}}
. Or else {{Px}} will be fed and return the string "{{{width}}}
" if width was not defined.
Background
[မႄးထတ်း]It used to be a bit tricky to handle empty but defined image size values in a template, since the MediaWiki pipetrick doesn't return the default value for empty parameters. That is, {{{width|250px}}}
does not return "250px" if the template was called like this: {{template|width=}}
The usual workaround was to do like this:
{{#if:{{{width|}}}|{{{width}}}|250px}}
When at the same time handling the "pxpx" bug this became even more messy code:
[[ၾၢႆႇ:{{{image}}} | {{#if:{{{width|}}}|{{Px|{{{width}}}}}|250px}} ]]
To simplify the coding, {{Px}} now allows for an optional default value to be defined allowing for the following:
[[ၾၢႆႇ:{{{image}}} | {{Px|{{{width|}}}|250}} ]]
Alternative approach
[မႄးထတ်း]Instead of letting your template take say the parameters image and size, you can build your template so it takes an image with full wiki notation. Like this:
{{your template | image = [[ၾၢႆႇ:Example.png|40px]] }}
This approach is used with many templates, for instance the {{Ambox}} and {{Ambox}}. This gives the users much more freedom in what they feed as the image parameter, and even allows such things as feeding two images at the same time. And it makes your template code much simpler.
Test examples
[မႄးထတ်း]{{px|40}}
= 40px{{px|40px}}
= 40px{{px|40pxpx}}
= 40pxpx{{px|junk}}
= junk{{px}}
={{px}}
={{px}}
={{px}}
={{px|40|50}}
= 40px{{px|50}}
= 50px{{px|50px}}
= 50px{{px|50|60}}
= 50px{{px|60}}
= 60px{{px|60px}}
= 60px{{px|frameless}}
= frameless{{px|frameless}}
= frameless{{px|40|frameless}}
= 40px{{px|frameless|40px}}
= frameless
[[ၾၢႆႇ:Example.png | thumb ]]
=
[[ၾၢႆႇ:Example.png | 50 | thumb ]]
=
- Fails; MediaWiki does not understand "50" and just thinks it is an image caption.
[[ၾၢႆႇ:Example.png | 50px | thumb ]]
=
[[ၾၢႆႇ:Example.png | {{Px|50}} | thumb ]]
=
[[ၾၢႆႇ:Example.png | {{Px|50px}} | thumb ]]
=
[[ၾၢႆႇ:Example.png | {{Px|junk}} | thumb ]]
=
[[ၾၢႆႇ:Example.png | {{Px|}} | thumb ]]
=
[[ၾၢႆႇ:Example.png | {{Px}} | thumb ]]
=
[[ၾၢႆႇ:Example.png | {{Px||50}} | thumb ]]
=
- Giving the default width if the 1st parameter is empty.
[[ၾၢႆႇ:Example.png | {{Px|{{{width|}}}|50}} | thumb ]]
=
[[ၾၢႆႇ:Example.png | {{Px|{{{width}}}|50}} | thumb ]]
=
- Fails to use the default value of 50. Note the missing "|" in {{{width}}}.
[[ၾၢႆႇ:Example.png | {{Px| {{{width|}}} | {{{size|}}} | 50 }} | thumb ]]
=