模板:Px/doc

来自Local Chinese Wikipedia
跳转到导航 跳转到搜索

脚本错误:没有“Message box”这个模块。 脚本错误:没有“High-use”这个模块。

This is the {{px}} template.

This meta-template 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:

脚本错误:没有“Tlg”这个模块。 = 40px
脚本错误:没有“Tlg”这个模块。 = 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:

脚本错误:没有“Tlg”这个模块。 = 50px
脚本错误:没有“Tlg”这个模块。 = 50px
脚本错误:没有“Tlg”这个模块。 = 60px
脚本错误:没有“Tlg”这个模块。 = 60px

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

[[File:{{{image}}} | {{px|{{{size|}}}}} | {{{caption|}}} ]]
[[File:{{{image}}} | {{px|{{{size|}}}|250}} | {{{caption|}}} ]]
[[File:{{{image}}} | {{px| {{{size|}}} | {{{width|}}} | 250 }} | {{{caption|}}} ]]

Default values[编辑]

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:

[[File:{{{image}}} | {{#if:{{{width|}}}|{{px|{{{width}}}}}|250px}} ]]

To simplify the coding, {{px}} allows for an optional default value to be defined allowing for the following:

[[File:{{{image}}} | {{px|{{{width|}}}|250}} ]]

Note! The parameters to 脚本错误:没有“Tlg”这个模块。 must use the pipe "|", like this: {{{width|}}}. Or else 脚本错误:没有“Tlg”这个模块。 will be fed and return the string "{{{width}}}" if width was not defined.

Background[编辑]

Old usage was to always pad on "px" since MediaWiki used to tolerate "40pxpx":

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

But since 25 March 2008 that does not work anymore. That is, image sizes on Wikipedia now needs to be given in the form "40px", not "40" or "40pxpx". This template helps doing that. Note that the bug has since been fixed, although you can continue using this template.

See also[编辑]

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

Test examples[编辑]

  • 脚本错误:没有“Tlg”这个模块。 = 40px
  • 脚本错误:没有“Tlg”这个模块。 = 40px
  • 脚本错误:没有“Tlg”这个模块。 = 40pxpx
  • 脚本错误:没有“Tlg”这个模块。 = junk
  • 脚本错误:没有“Tlg”这个模块。 =
  • 脚本错误:没有“Tlg”这个模块。 =
  • 脚本错误:没有“Tlg”这个模块。 =
  • 脚本错误:没有“Tlg”这个模块。 =
  • 脚本错误:没有“Tlg”这个模块。 = 40px
  • 脚本错误:没有“Tlg”这个模块。 = 50px
  • 脚本错误:没有“Tlg”这个模块。 = 50px
  • 脚本错误:没有“Tlg”这个模块。 = 50px
  • 脚本错误:没有“Tlg”这个模块。 = 60px
  • 脚本错误:没有“Tlg”这个模块。 = 60px


Currently fails. But the devs have stated that they are adding back parsing of "pxpx" in the next update to Wikipedia. So if you see a small image to the right then Wikipedia again accepts "pxpx".








Giving the default width if the 1st parameter is empty



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


  • [[File:Example.png | {{px| {{{width|}}} | {{{size|}}} | 50 }} | thumb ]] =
    File:Example.png