Kigezo:Px

Kutoka Wikipedia, kamusi elezo huru



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[hariri chanzo]

Here is what this template does. Code to the left, actual rendering to the right:

{{px|40}} = 40px
{{px|40px}} = 40px

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:

{{px||50}} = 50px
{{px||50px}} = 50px
{{px|||60}} = 60px
{{px|||60px}} = 60px

And here is how it can be used inside another template:

[[Image:{{{image}}} | {{Px|{{{size|}}}}} | {{{caption|}}} ]]
[[Image:{{{image}}} | {{Px|{{{size|}}}|250}} | {{{caption|}}} ]]
[[Image:{{{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[hariri chanzo]

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:{{{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:{{{image}}} | {{Px|{{{width|}}}|250}} ]]

The pxpx bug[hariri chanzo]

Old usage was to always pad on "px" since MediaWiki used to understand "40px" and "40pxpx", but not "40":

[[Image:{{{image}}} | {{{size}}}px | {{{caption|}}} ]]

But after 25 March 2008 that did not work anymore. That is, image sizes on Wikipedia needed to be given in the form "40px", not "40" or "40pxpx". This template helps doing that. Note that the bug has since been fixed, so MediaWiki now understands "40px" and "40pxpx" again. But MediaWiki still does not understand "40".

But feel free to continue using this template since it is more convenient to use than the old methods.

Alternative approach[hariri chanzo]

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 = [[File:Example.png|40px]]
}}

This approach is used with many templates, for instance the {{ambox}} and {{dmbox}}. 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.

See also[hariri chanzo]

  • {{ifempty}} – Helps detect empty parameters and fall back to default values. For instance for handling multiple image name parameters.
  • Wikipedia:ClickFix – About the old "pxpx" bug.

Test examples[hariri chanzo]

  • {{px|40}} = 40 px
  • {{px|40px}} = 40px
  • {{px|40pxpx}} = 40pxpx
  • {{px|junk}} = junk
  • {{px}} =
  • {{px|}} =
  • {{px||}} =
  • {{px|||}} =
  • {{px|40|50}} = 40 px
  • {{px||50}} = 50 px
  • {{px||50px}} = 50px
  • {{px||50|60}} = 50 px
  • {{px|||60}} = 60 px
  • {{px|||60px}} = 60px
  • {{px|frameless}} = frameless
  • {{px||frameless}} = frameless
  • {{px|40|frameless}} = 40 px
  • {{px|frameless|40px}} = frameless
  • [[Image:Example.png | thumb ]] =


  • [[Image:Example.png | 50 | thumb ]] =
    50
Fails, MediaWiki does not understand "50" and just thinks it is an image caption.


  • [[Image:Example.png | 50pxpx | thumb ]] =
Used to fail. But the devs added back parsing of "pxpx". So you should see a small image to the right without any image caption, since Wikipedia again accepts "pxpx".


  • [[Image:Example.png | 50px | thumb ]] =


  • [[Image:Example.png | {{Px| 50 }} | thumb ]] =


  • [[Image:Example.png | {{Px| 50px }} | thumb ]] =


  • [[Image:Example.png | {{Px| junk }} | thumb ]] =
    junk


  • [[Image:Example.png | {{Px| }} | thumb ]] =


  • [[Image:Example.png | {{Px}} | thumb ]] =


  • [[Image:Example.png | {{Px| | 50 }} | thumb ]] =
Giving the default width if the 1st parameter is empty


  • [[Image:Example.png | {{Px| {{{width|}}} | 50 }} | thumb ]] =


  • [[Image:Example.png | {{Px| {{{width}}} | 50 }} | thumb ]] =
    {{{width}}}
Fails to use the default value of 50. Note the missing "|" in {{{width}}}.


  • [[Image:Example.png | {{Px| {{{width|}}} | {{{size|}}} | 50 }} | thumb ]] =
https://sw.wikipedia.org/wiki/Kigezo:Px
sw.wikipedia.org
CC-MAIN-2023-50
72646
application/http; msgtype=response
sha1:C3NWI245BED63FD4GAK4C5IG4K66FWXV
<urn:uuid:6042c709-e723-4d23-86c0-783b802dc237>
2023-12-09T06:14:38Z
208.80.154.224
text/html
sha1:SXGLAAO4QNLRGKNUB26HIEJE4COSJ76R
<urn:uuid:d827139d-0cb8-4f4a-9b0f-d1c4ba8f5112>
https://sw.wikipedia.org/wiki/Kigezo:Px
response
<urn:uuid:df420d7c-1121-4376-abea-d9493ad844be>
isPartOf: CC-MAIN-2023-50
publisher: Common Crawl
description: Wide crawl of the web for November/December 2023
operator: Common Crawl Admin (info@commoncrawl.org)
hostname: ip-10-67-67-156
software: Apache Nutch 1.19 (modified, https://github.com/commoncrawl/nutch/)
robots: checked via crawler-commons 1.5-SNAPSHOT (https://github.com/crawler-commons/crawler-commons)
format: WARC File Format 1.1
conformsTo: https://iipc.github.io/warc-specifications/specifications/warc-format/warc-1.1/