Template talk:Px
From Wikipedia, the free encyclopedia
Contents |
[edit] About this code
This template is based on code that MZMcBride added to {{Click}}. But I packaged it as {{px}} to make it easier to use.
What this code does is to check if the parameter is a number, if it is then it returns the parameter + "px", if it is not a number then it returns the parameter unchanged. Thus "40" becomes "40px", while "40px" is unchanged.
--David Göthberg (talk) 10:54, 27 March 2008 (UTC)
[edit] Problem if parameter undefined
- I have now repaired the infobox showed here, thus the third example does not exhibit the error anymore. --David Göthberg (talk) 18:05, 27 March 2008 (UTC)
If one has in a template [[Image:{{{image}}}|{{px|{{{width|250}}}}}]], then this owrks fine if "width=250" or "width=250px" in the article page calling the template. But if the article does not define the width parameter, as in "width = ", (ie is relying upon the default value) then this does not seem to show a picture at all.
{{drugbox | image = flucloxacillin.png }}
Works fine when no mention of width parameter at all
Px
|
|
Systematic (IUPAC) name | |
? | |
Identifiers | |
CAS number | ? |
ATC code | ? |
PubChem | ? |
Chemical data | |
Formula | ? |
Mol. mass | ? |
Pharmacokinetic data | |
Bioavailability | ? |
Metabolism | ? |
Half life | ? |
Excretion | ? |
Therapeutic considerations | |
Pregnancy cat. |
? |
Legal status | |
Routes | ? |
{{drugbox | image = flucloxacillin.png | width = 100 }}
Likewise fine if width defined
Px
|
|
Systematic (IUPAC) name | |
? | |
Identifiers | |
CAS number | ? |
ATC code | ? |
PubChem | ? |
Chemical data | |
Formula | ? |
Mol. mass | ? |
Pharmacokinetic data | |
Bioavailability | ? |
Metabolism | ? |
Half life | ? |
Excretion | ? |
Therapeutic considerations | |
Pregnancy cat. |
? |
Legal status | |
Routes | ? |
{{drugbox | image = flucloxacillin.png | width = }}
But undefined parameter causes problems
Px
|
|
Systematic (IUPAC) name | |
? | |
Identifiers | |
CAS number | ? |
ATC code | ? |
PubChem | ? |
Chemical data | |
Formula | ? |
Mol. mass | ? |
Pharmacokinetic data | |
Bioavailability | ? |
Metabolism | ? |
Half life | ? |
Excretion | ? |
Therapeutic considerations | |
Pregnancy cat. |
? |
Legal status | |
Routes | ? |
Solutions in use of this template (with respect to Template:Drugbox example given) ? David Ruben Talk 23:33, 26 March 2008 (UTC)
Okay, I'll investigate that. Can't be too hard to fix. I have worked a lot with detecting empty and non-defined parameters. --David Göthberg (talk) 08:30, 27 March 2008 (UTC)
Ouch! That was a sneaky bug. Took me some time to even be able to replicate your bug in my own code. Had to stare a lot at the code in {{drugbox}} and test many variants in my own code. But first of all, you do not need to use {{!}}
for "|
" if inside images and template calls, even if that code is within a wikitable. So I will be using "|
" in these examples since that will work for you too.
This bug doesn't really depend on {{px}}. It seems that MediaWiki is parsing image tags worse than we thought. This might be a new bug that came with the new MediaWiki version on 25 March 2008 since we have not seen it before. (At least I have never noticed it before.)
Here is code with the bug:
[[Image:Example.jpg|{{{width|100px}}}]]
If it gets a not defined width parameter it will display the image fine, but if it gets an empty width parameter then it will render this:
- [[Image:Example.jpg|]]
And here is code that always will render the image:
[[Image:Example.jpg | {{{width|100px}}} ]]
As you can see, the only difference is the spaces!!! Now that is what I call bad parsing.
Oddly enough if the code uses numbered parameters instead then it doesn't fail as often. So this mostly works:
[[Image:Example.jpg|{{{1|100px}}}]]
We have to test more to know exactly when what happens.
Anyway, what you need to do is to add some spaces to your image code in {{drugbox}} and you should be fine. (I can't fix it for you since that template is locked.)
--David Göthberg (talk) 10:23, 27 March 2008 (UTC)
-
- Thanks for fix to drugbox, but when I tried exactly the same code at {{Infobox Shinto shrine}} (see this version):
- [[Image:{{{image}}}| {{px|{{{width|250}}} }} ]]
- an empty width parameter ended up showing the image at its original size (unlike the above previous problem with Drugbox where no image was shown at all). My fix around this is to specifically test for empty value with:
- [[Image:{{{image}}}| {{px|{{#ifeq: {{{width|}}} | | 250 | {{{width|}}} }} }}]]
- i.e. I need replace what should be a central {{width|250}} with |{{#ifeq: {{{width|}}} | | 250 | {{{width|}}} }}
- so if width equals blank/empty, then set as 250, else as width value - then all this passed over to px function.
- This though is quite horrible coding - what on earth is the point in the use of pipes for alternative values if mediawiki goes and treats empty and null values as somehow different. Is solution to make mediawiki treat empty & null parameters as the same and in so doing cut a swath through needless convolutions (it is not as if mediawiki need be a programming language where a null variable is one that already has a memory allocated for it vs an empty parameter which has yet to be assigned memory space)
- So why does problem occur in one template usage but not the other, is there a more consistant form of coding which will work all the time and thirdly how about a sensible mediawiki handling of empty/null parameters ? David Ruben Talk 02:49, 28 March 2008 (UTC)
- Thanks for fix to drugbox, but when I tried exactly the same code at {{Infobox Shinto shrine}} (see this version):
-
-
- Ouch, right you are. Thus {{drugbox}} too will currently not set the wished default size for the images if it gets an empty width parameter. I missed that, even though I know how it works. Tricky stuff.
- And I agree that it is silly that the notation
{{{width|250}}}
only returns 250 for null parameters, but returns an empty string if it was fed an empty parameter. - By the way, I usually find it simpler to use "if" instead of "ifeq" to check for empty+null parameters. Like this:
{{#if:{{{width|}}}|{{{width}}}|250}}
- Which in the image case becomes:
[[Image:{{{image}}} | {{px|{{#if:{{{width|}}}|{{{width}}}|250}}}} ]]
- Or probably better:
[[Image:{{{image}}} | {{#if:{{{width|}}}|{{px|{{{width}}}}}|250px}} ]]
- Oh, that gave me some ideas. I'll go and test them and come back when I know if they work.
- --David Göthberg (talk) 06:14, 28 March 2008 (UTC)
-
This does not appear to be a related problem, as far as I can tell with some quick review. File a bug. [[Image:Example.png|]] works, but [[Image:Example.png|{{{x|}}}]] does not (when x is not set). The code paths don't even reach Parser::makeImage() in this case. Tim Starling is the one I would ask about this. —Simetrical (talk • contribs) 18:55, 28 March 2008 (UTC)
[edit] Broken
Sad to say that this template is broken. {{px|40x30px}} correctly gives 40x30px but {{px|40x30}} incorrectly gives 40x30. /Lokal_Profil 22:52, 27 March 2008 (UTC)
- Not really, both "40x40px" and "40x40" is according to MediaWiki notation not an image size parameter at all, that is instead just "text", that is image captions. And there is no way we could make a template that processes those strings into what you would need, that is "40px". Since MediaWiki doesn't have functions for such string handling.
- What the {{px}} template does is to take "40" or "40px" and returns them as "40px", nothing else. Sorry. I would have liked to accept "40pxpx" too and turn it into "40px", but we can not do that in a template. (Well, there is a nasty workaround based on an idea by Happy-melon that involves using 1000 subpages that actually can do it. But he got pretty bashed for creating 1000 subpages just to detect a number between 1pxpx and 1000pxpx.) But word is from the devs that they are now fixing MediaWiki so it will accept both "40px" and "40pxpx" as valid image size parameters. It will be implemented in the next software update of Wikipedia.
- --David Göthberg (talk) 06:31, 28 March 2008 (UTC)
-
- According to Wikipedia:Extended image syntax "{width}x{height}px" is a allowed image size (and I know from experience it works). Have changed the example above to make it clearer. /Lokal_Profil 15:33, 28 March 2008 (UTC)
-
-
- Oh dear, I checked. You are right, 40x30px is valid syntax nowadays. I didn't know that. He, not a good excuse, but I have to blame that I've been so long on Wikipedia that the year I learnt to use images was before that option was added.
- But anyway, there is nothing we can do about it. Since template programming lacks string handling so there is no way we can detect the difference between 40x30 and 40x30px. (Both are strings, none of them is a number.)
- So anyone that uses a template that uses {{px}} and that wants to feed both max width and max height of the image has to use the "px" ending. That is, they have to write "size=40x30px" when they call their template. While the rest that usually only set width still can write both 40 or 40px. I hope that 40x30px usage is as rare as I think it is, since I have never seen it used. So I hope that not many pages are affected.
- But the word from the devs is that they are adding back the functionality that "40pxpx" should be valid syntax too, and let's hope they add that "40x30pxpx" will be valid too. Because then we can do the simpler tacking on of "px" no matter what, like most templates did before. They say the fix will be implemented in the next upgrade of Wikipedia.
- --David Göthberg (talk) 17:48, 28 March 2008 (UTC)
-
-
-
-
-
- Ah good. But since only übergeeks read regex, could you give some examples of what would be valid inputs for image size when this patch has been applied?
- --David Göthberg (talk) 20:13, 28 March 2008 (UTC)
-
-
-
-
-
-
-
-
- The following will be considered a valid width/height combination:
^([0-9]*)x([0-9]*)\s*(?:px)?\s*$
That means a string exactly consisting of zero or more digits, followed by a lowercase 'x', followed by zero or more digits, followed by zero or more whitespace characters, followed optionally by the string "px", followed by zero or more whitespace characters. This must be followed by the mandatory "px" string ― so up to two are allowed, as requested. The regex for width alone is the same, except that it doesn't allow the 'x'. —Simetrical (talk • contribs) 01:54, 30 March 2008 (UTC)
- The following will be considered a valid width/height combination:
-
-
-
-
-
-
-
-
-
-
- Ah thanks for clarifying that. I had no idea that whitespace was allowed too. Always more to learn...
- --David Göthberg (talk) 07:26, 30 March 2008 (UTC)
-
-
-
-
-
[edit] Handling empty values
Currently it is a bit tricky to handle empty but defined image size values in a template. 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 is to do like this:
{{#if:{{{width|}}}|{{{width}}}|250px}}
When at the same time handling the "pxpx" bug this becomes even more messy code:
[[Image:{{{image}}} | {{#if:{{{width|}}}|{{px|{{{width}}}}}|250px}} ]]
Because of that I would like to include such handling in {{px}} itself. Then it could be used like this:
[[Image:{{{image}}} | {{px|{{{width|}}}|250}} ]]
Much nicer, isn't it? Here is the new code for {{px}}:
<includeonly>{{#if:{{{1|}}} | {{#iferror: {{#expr:{{{1|}}} > 0 }} | {{{1|}}} | {{{1|}}}px }} | {{#iferror: {{#expr:{{{2|}}} > 0 }} | {{{2|}}} | {{{2|}}}px }} }}</includeonly><noinclude> {{pp-template|small=yes}} {{documentation}} <!-- Add categories and interwikis to the /doc subpage, not here! --> </noinclude>
This code does not affect templates that already use {{px}} and I have tested this code carefully in my own sandbox. (Very annoying that I can not update templates that I created myself.)
--David Göthberg (talk) 10:35, 28 March 2008 (UTC)
-
- David Göthberg, I have now added the clear description of empty parameter values and the px's optional 2nd parameter to the px/doc. David Ruben Talk 22:21, 28 March 2008 (UTC)
-
-
- Ah, thanks Happy-melon and David Ruben. And the test examples you added shows that it works fine.
- --David Göthberg (talk) 03:22, 29 March 2008 (UTC)
-
Oh! David Ruben: I just saw what you did to {{drugbox}}. You used {{px}} with nesting to handle two different width values and a default value. Very neat. I hadn't thought of that possibility. For the rest of you, this is kind of the code he added:
{{px| {{{width|}}} | {{px| {{{size|}}} | 220 }} }}
It will first try "width", if that one is empty or null then it will try "size", and then the default "220".
Since we now knows this case occurs then we should perhaps add it into {{px}} itself so we simply can write:
{{px| {{{width|}}} | {{{size|}}} | 220 }}
Should I code it up and test it?
--David Göthberg (talk) 03:45, 29 March 2008 (UTC)
-
- Yes please, certainly would be neater. I presume just needs an optional 3rd parameter not unlike {{ifempty}}, but I think the 3 levels will be enough in templates (in drugbox used to allow sizing for 2 pictures: for value for image2, else value for image1 if only that specified else default if no width specified) David Ruben Talk 04:22, 29 March 2008 (UTC)
{{editprotected}}
Yes I agree, four parameters might be a bit bloated and unlikely to be used. So here is the new code for {{px}} that can handle three parameters. Of course properly tested in my own user space.
<includeonly>{{#if:{{{1|}}} | {{#iferror: {{#expr:{{{1|}}} > 0 }} | {{{1|}}} | {{{1|}}}px }} | {{#if:{{{2|}}} | {{#iferror: {{#expr:{{{2|}}} > 0 }} | {{{2|}}} | {{{2|}}}px }} | {{#iferror: {{#expr:{{{3|}}} > 0 }} | {{{3|}}} | {{{3|}}}px }} }} }}</includeonly><noinclude> {{pp-template|small=yes}} {{documentation}} <!-- Add categories and interwikis to the /doc subpage, not here! --> </noinclude>
Now let's see how long it takes before someone comes here asking for a fourth parameter. :))
--David Göthberg (talk) 05:50, 29 March 2008 (UTC)
-
-
- Thank you. And I have updated the docs and test examples to show the third parameter. Guess this template now has become so nifty that we will continue to use it even when the "pxpx" bug has been fixed in MediaWiki.
- --David Göthberg (talk) 17:47, 29 March 2008 (UTC)
-
[edit] Please Help
I've recently set up a (MediaWiki) wiki at my company and am having major problems with this. When I add an image, it is displayed fine. But the image is too big. So I've added variants using "px" and none of them display the image! I'd really like to scale it down. (Can't MediaWiki do that automatically?) I'm pretty stuck here so would very much appreciate some help! Thanks. Robinson weijman (talk) 14:59, 9 June 2008 (UTC)
- Not familar with MediaWiki myself, but how about thumb parameter in image display, does that help ? David Ruben Talk 17:39, 9 June 2008 (UTC)
[[Image:test.png|200px|right]]
[[Image:test.png|thumb|right|200px]]
-
- Thanks for the reply, Dave. No, that doesn't help: when I use that code nothing is displayed - no text, image or anything! All I can do is, e.g. [[Image:test.png]] and then the full (too large) image is displayed. Robinson weijman (talk) 08:57, 11 June 2008 (UTC)
-
-
- Am I required to have ImageMagick installed to do this? Robinson weijman (talk) 13:45, 12 June 2008 (UTC)
-
-
-
-
- No idea to your last question and clearly not issue of {{px}} template but of your settup of MediaWiki, so wrong place to be getting help for this... That said, see (I guess you already have) article ImageMagick but also on MediaWiki http://www.mediawiki.org/wiki/Manual:%24wgImageMagickConvertCommand
- After that look at http://www.mediawiki.org/wiki/Manual:FAQ and in particular:
- If still no joy then try http://www.mediawiki.org/wiki/Project:Support_desk :-) David Ruben Talk 14:14, 12 June 2008 (UTC)
-
-
-
-
-
-
- Well thanks, David, for the thorough answer. Very kind of you. I won't be able to try out your solution for a few days now but when I do I'll get back to you. Robinson weijman (talk) 18:05, 12 June 2008 (UTC)
-
-
-