Help talk:Advanced templates

From Meta, a Wikimedia project coordination wiki
Jump to: navigation, search

Including the content of a template in a template or variable name or parameter value[edit]

According to this section, this has been possible since 19 October 2005. I have just upgraded to MediaWiki 1.5.1 which was built after this date, however I cannot include templates within the parameters of other templates. DO I need to grab specific files out of CVS? -- Colin E.

The description refers to the version Wikimedia uses, which is now 1.6alpha.--Patrick 00:23, 3 November 2005 (UTC)

Help with Missing Parameters[edit]

I'm a programmer, and I find these templates so esoteric it's impossible to use them. So I'm asking for help. I want to make an infobox template that has an image file name as a parameter, among others. If the image name is missing, I want to omit some of the text from the template (a table row). If you can give me a simple example I'll be grateful, either here or on my Talk page. If there's a better forum for this request, please let me know. Thanks very much, —Papayoung 71.208.44.105 22:20, 29 September 2005 (UTC)

Handling missing parameters is not possible, handling blank ones is. See Template talk:Ine.--Patrick 01:05, 1 October 2005 (UTC)
You're wrong! See below :-P R 21:38, 20 October 2005 (UTC)

Change in syntax[edit]

There's just been a very important change concerning templates.

  • There is now a syntax to define a default value for parameters, which is used when the parameter is undefined. It has the form {{{param|default value}}}.
  • It is now possible to use templates in parameter values.

Thus, a significant part of this page must be rewritten! R 21:36, 20 October 2005 (UTC)

Little correction[edit]

Following excerpt is not exacty true:

It is not possible to use a template with a name dependent on the content of another template.

For example: {{{{tctc}}}} is trying to include a template on a page, using the text in Template:Tctc containing "tc" to get the name of the template. However, {{{{tctc}}}} simply gives the text {{{{tctc}}}}, not the content of Template:tc containing "in<noinclude>{{documentation}}</noinclude>".

Example is correct, but statement "It is not possible to use a template with a name dependent on the content of another template" is incorrect as from 19 October, 2005 it is possible to make template name depend on value of other template, you just need to separate closing parentheses with a space or '|' ({{{{tctc}}|}} or {{{{tctc}} }} or {{ {{tctc}}}}). Knutux 09:21, 24 October 2005 (UTC)

Thanks, I changed it.--Patrick 19:30, 24 October 2005 (UTC)

Retrive parameter depending on other parameters value[edit]

Is that in 1.5.x or in 1.6alpha?

Cleanup[edit]

This help page needs a major cleanup I think AzaToth 15:36, 2 December 2005 (UTC)

I agree. This "documentation" borders on incoherent. ᓛᖁ♀ 16:42, 16 December 2005 (UTC)
Agreeing, and adding the cleanup tag. (I don't know how much the page has changed in the last year, but the above comments still seem to apply.) -Starwed

Difference between writing parameters directly or keeping them in a template[edit]

I have a small problem. If I have a template and define a parameter in it on the page I'm using it on, I can also define other parameters, which the template doesn't use, without its showing on the page. However, if I keep the definitions in another template, it insists on listing all the other, unused parameters on the page. Why is that?
Let me explain it to make you understand it better (this is an extreme simplification of my situation):
I have a template called Do. It contains the following: Start-{{{Parameter1}}}-end. If I use it on a page by writing {{Do|Parameter1=Middle}} it will of course show up on the page as Start-Middle-End. I can also define a lot of unused parameters, so as to write {{Do|Parameter1=Middle|Parameter2=To|Parameter3=Till}} and it still will show up on the page as Start-Middle-End. So far so good. However, if I define all these parameters in another template called Do Definitions (which then contains Parameter1=Middle|Parameter2=To|Parameter3=Till) and I use the template Do by writing {{Do|{{Do Definitions}}}}, shouldn't the Do template interpret this as {{Do|Parameter1=Middle|Parameter2=To|Parameter3=Till}}? It doesn't seem to, since it turns up on the page as Start-Middle|Parameter2=To|Parameter3=Till-End. Why is that? Can't I use another template in which I define a lot of unused parameters (for other use)? In short: If I have written something in a template and include it in another template, why doesn't the second template interpret the text in the first template exactly as if I had written the text in the second template? /85.224.55.111 15:48, 20 January 2006 (UTC)

I added this to Help:Advanced_templates#A_template_containing_an_assignment_of_a_parameter_value_to_a_parameter_name. Apparently identifying separate parameter assignments is done before evaluating templates in each.--Patrick 00:09, 23 January 2006 (UTC)

Mimicking a database table[edit]

In the w:Polyhedron pages we have a number of pages where the same information is repeated in several places. We would like to do this as there are several different views of the subject which group the data in different ways exhibiting different relationships e.g. w:List of uniform polyhedra by Wythoff symbol, w:List of uniform polyhedra by vertex figure and w:List of Wenninger polyhedron models. We have 75 different polyhedra we would like to display which have about 10 properties each.

Rather than try to maintain all the data in the different pages allowing lots of transcription errors, we've though of using a simple database approach. I've tried two different approaches, one using a {{switch}} and one with a sort of callback mechanism. An example of this can be found at w:User:Pfafrich/test.

In the callback version there is one template per polyhedron. For example w:Template:Polyhedra T contains

{{{{{1}}}|name=Tetrahedron|image=tetra.png|....}}

Theres also a few different templates defining how the polyhedron is displayed w:Template:Polyhedra row contains code like a normal info box

<tr><td>[[{{{name}}}]]</td>
<td>[[Image:{{{image.png}}}]]</td></tr>

To call it we use code like

{{Polyhedra T|Polyhedra row}}

In the switch verion there one template with the data

{{switch|{{{poly}}}
|case: T name=tetra|case: TF=4|case: TE=6|case: TV=4
|case: O name=octa|case: OF=8|case: OE=12|case: OV=6
|case: H name=hexa|case: HF=6|case: HE=12|case: HV=8
|case: I name=icosa|case: IF=20|case: IE=30|case: IV=12
|case: D name=Doceca|case: DF=12|case: DE=30|case: DV=20
}}

An another w:Template:User pfafrich test chi which displays the data

'''{{User pfafrich test|poly={{{1}}} name}}'''
Faces: {{User pfafrich test|poly={{{1}}}F}}
Edges: {{User pfafrich test|poly={{{1}}}}E}}
Vertices: {{User pfafrich test|poly{{{1}}}}V}}

This uses a composition of parameter values and a lot of calls to switch. This would be called like

{{User pfafrich test chi|T}}

I'm wondering which technique is better in terms of server load. I've a feeling the callback method is more elegant. However it would be easier to have the data in one big table, rather than in 75 different templates.

Anyone have advice on a nice way to do this? --Salix alba 02:06, 22 January 2006 (UTC)

I must admit that I'm suprised Wiki doesn't do lookups very well. I have a system where I want to display the same information on several pages that are related to each other so I have to either manually edit each of those pages or I put the common stuff onto a template page. This is OK when you are only talking about a few templates but when you get up to over 200 templates its a complete pain to maintain. I'd love to be able to have ONE template that I pass a parameter to and that returns the common data for that parameter Steve A 15:49, 20 February 2006 (UTC)

Each polyhedron is included with code like

{{Polyhedra DB|Polyhedra smallbox2|T}}

Where T is the name of the polyhedra, in this case Tetrahedron.

w:Template:Polyhedra DB is like

{{{{{1}}}|{{{2}}}|

|T-name=Tetrahedron|T-image=tetrahedron.jpg| # name and image
|T-V=4|T-E=6|T-F=4|   # number of corners, edges and faces
                      # other data omitted

|O-name=Octahedron|O-image=octahedron.jpg|
...
}}

The data is encoded like T-image=data where T is the name for lookup (i.e. row), and image is particular property (i.e. column).

The first two parameters to this template just pass their arguments through, so this resolves to

{{Polyhedra smallbox2|T|T-name=Tetrahedron|....}}

and means that the Polyhedra smallbox2 template is included. Now Polyhedra smallbox2 is like:

[[Image:{{{{{{1}}}-image}}}|100px]]<BR>
[[{{{{{{1}}}-name}}}]]<BR>
V {{{{{{1}}}-V}}},E {{{{{{1}}}-E}}},F {{{{{{1}}}-F}}}

so with the first parameter T this becomes

[[Image:{{{T-image}}}|100px]]<BR>
[[{{{{T-name}}}]]<BR>
V {{{T-V}}},E {{{T-E}}},F {{{T-F}}}

Just like a standard infobox template. The system is quite flexable as different display templates can be used to display different data. --Salix alba 00:23, 14 March 2006 (UTC)

A parameter name depending on a parameter[edit]

In this section, I found the very neat options template (Do if parameter is empty and Do if parameter isn't empty). Can this be done in the way of Do if parameter contains a and Do if parameter contains b instead? I can't figure it out for myself. /85.224.55.111 22:40, 23 January 2006 (UTC)

No, there is no "contains" function/relation.--Patrick 00:14, 22 February 2006 (UTC)

Using Templates in Extensions[edit]

I am using an iframe extension because I would like to include the Amazon iframes on my main pages for books and movies. Is it possible for me to put the basic iframe code in a template and then call the template passing only the isbn number and have it correctly insert the parameter in the iframe?

My template code is as follows:

<iframe> url=http://rcm.amazon.com/e/cm?t=epinionaddicts&o=1&p=8&l=as1&asins={{{asin}}}&fc1=000000&IS2=1&lt1=_blank&lc1=0000ff&bc1=000000&bg1=ffffff&f=ifr width=120px height=240px name=amazon </iframe>

And, I would like to call it like this:

{{Amazon | asin=0393324826}}

But, when I do this, the iframe shows without the parameter being in its proper place. The asin never seems to be parsed.

Am I missing something?


Hyperlink in a template included in a template[edit]

Hello,

after upgrading to wikimedia 1.6.1 I tried to use the "template in template" functionality. Everything is OK except for the following example.

I use the example {{x3|{{x5|{{tc}}}}}} from the section Including the content of a template in a template ...

I modify the example to {{x3|{{x5|[http://service.sap.com/~form/handler?_APP=01100107900000000342&_EVENT=REDIR&_NNUM= SAP]}}}} and get this {{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}{{{1}}}.

I am not sure about the cause, but I think it´s something with the Hyperlink, cause {{x3|{{x5|[http://www.google.de Google]}}}} is working fine (GoogleGoogleGoogleGoogleGoogleGoogleGoogleGoogleGoogleGoogleGoogleGoogleGoogleGoogleGoogle).

Any ideas? tia, Marcus

Returning just first letter of a parameter?[edit]

I've been wondering if this is even possible. Say I have a template which accepts 3 parameters. I want to take one of those parameters, and use just the first letter, while still keeping the parameter intact for further use.

Can it be done? - Superwad

You might want to check the StringFunctions extension. - Manu3d 09:43, 11 July 2007 (UTC)

Optional table row[edit]

I'm trying to make a template that has table formating(IE Background and Border Colour) and changes depending on what someone enters.

The Table Code is this:

{|
|-
! colspan="3" style="border:1px solid #a3bfb1; background:#cef2e0;"| Persons Name :
| style="border:1px solid #a3bfb1; background:#f5fffa;"|{{{name}}}.
|-
! colspan="3" style="border:1px solid #a3bfb1; background:#cef2e0;"| Person's place of Birth :
| style="border:1px solid #a3bfb1; background:#f5fffa;"|{{{Place of Birth}}}.
|}

So if someone wanted to use this, they only have to enter:

{{Template Person
|name= Bob
|Place of Birth= Far Away
}}

And it will show that information. But could someone tell me is it possiable to make it so if someone puts only name, the place of Birth row in the table dosn't show?

With Place of Birth With Out Place of Birth
Persons Name : Bob.
Person's place of Birth : Far Away.
Persons Name : Bob.

Please bare with me, I'm still learning... And sorry if this isn't the right place...Didn't know.

A special Template:! is used, and ParserFunctions, use the second wikitext (here showing the result with undefined parameter), the first shows what you get when the parameter is defined:
Persons Name : Bob.
Person's place of Birth : Far Away.
Persons Name : Bob.
Patrick 11:57, 5 September 2006 (UTC)
Thank you for the help.
Hi, I am also interested in the optional table row but don't quite understand the above statement. Could anyone help me out?

Thanks - RM

Template for: Is this article belong to a specifiv category[edit]

Can one write a template in order to find out, whether the current article belongs to a specific category? JanCK 23:47, 21 October 2006 (UTC)

Link to information on #if, #expr etc.[edit]

An anonymous IP user changed a link in the first paragraph from "Parserfunctions" to "Parser function" with edit summary "fixed broken link". The "Parserfunctions" link which I originally put in works for me and leads to a page with information on "#if:" and "#expr:" etc. which is needed for anyone wanting to do template programming. The link "Parser function" leads to a different page. Both links are needed, and the "Parser function" link was already given in the previous sentence. I'm putting back the link I originally put in. --Coppertwig 12:33, 3 April 2007 (UTC)

Variable number of parameters[edit]

I'm trying to make a few templates that essentially amount to differently formatted Q&A pages. They're prime for templates because they're all basically the same thing repeated over and over: A bunch of question and answer combos in a common format. So, obviously, the question should be a template, the answer should be a template, the Q/A combo should be a template made up of the question template and answer template, and then the entire Q&A page itself should be a template made up of some number of Q&A combo templates.

The problem is that some Q&A pages may have 5 Q/A combos, some 10, some 100. So here's the problem: Is there any way to make a template that can have 1 to n possible parameters without doing hundreds or thousands of lines of {{#if: {{{1|}}} | do stuff}} {{#if: {{{2|}}} | do more stuff}} ... {{#if: {{{250|}}} | do even more}}?

I've seen Template:for, and I know how to use it directly (like make a table on one page with a variable number of rows/cells), but I'm not really sure how to incorporate that into a template to be used on multiple pages; I.e. pass the Q&A template a random number of anonymous parameters which it then passes to Template:for, which calls the Q/A combo template once for each. The only way I can see to do it is to take the actual code from Template:for and implement it in every single template that could have a variable number of parameters, which is a terrible solution.

Any suggestions would be appreciated. --20.4.235.68 20:28, 2 December 2009 (UTC)

Use values from included templates?[edit]

How can I include the value of a used template from an article in another article? For example, the Wikipedia article "Alfred Hitchcock" uses the Persondata template. In another article, I would like to insert the date of birth that is given in that article. If it is not possible using the template technique, does anyone know of an extension that is capable of doing so?

Anyone?

<pre> Challenge[edit]

I'm trying to set up an {{WExample}} template to simplify help pages on MediaWiki syntax and templates documentation. Normally, to get something like

You type You get

this will be '''bold'''

this will be bold


you have to type the code twice, once between <pre></pre> tags, and once without. {{WExample}} should do this automatically. Now it works as long as its parameter doesn't require a transclusion, so for example it cannot be used on itself :

You type You get

{| class="wikitable" ! You type !! You get |- | this will be '''bold''' | this will be '''bold''' |}

You type You get

this will be '''bold'''

this will be bold


The source isn't correct on the leftmost side. Who will be able to solve this will get my eternal gratefulness, and his name engraved in the history of this very useful template ;-)

--Goulu 08:19, 17 May 2010 (UTC)

Parameter name from another parameter in the same template[edit]

I'm struggling to see how {{ppp|p=foo|foo=bar}} is different from {{#switch:foo|foo=bar}}. Could someone clarify this for me? Thanks. Smith609 04:25, 20 November 2010 (UTC)

See Help:Array.--Patrick (talk) 10:33, 20 November 2010 (UTC)
Very helpful link; thanks for pointing me that way. Smith609 22:30, 27 November 2010 (UTC)

subfunctions[edit]

Do we have a tutorial for calling a subfunction?

Basically, if a field has a certain value, I want it modified with a template, but otherwise to just return the value. (It appears in too many places for it to be economical to modify it within the main template.)

If I'm not being clear, I want to pass the {{{ref}}} field to template/ref. I've tried things like the following:

{{#ifeq: {{{1}}} | keyvalue 
 | <ref>{{Ethnologue| {{{iso3}}} }}</ref>
 | {{{1}}}
 }}

The default works fine, but when I use the key value, the 'Ethnologue' template takes the string {{{iso3}}} as its input, rather than the value of the iso3 field. I can't figure out how to pass back the field rather than the string.

On a possibly related note, do we have a description of the difference between {{{1}}}, {{{1|}}}, and {{{1|-}}}? Everything I've found is either more basic or more advanced than that, so I'm stuck in the middle. Kwamikagami 22:08, 16 November 2011 (UTC)

See Help:Template#When_parameters_do_not_expand.--Patrick (talk) 00:51, 17 November 2011 (UTC)

Ah, thanks. That makes sense. But it rather than just returning a 'ref' tag, it also adds a 'ref' field to the info box. And it still returns the internal fields as strings. What I now have:

 {{#ifeq: {{{1}}} | keyvalue 
  | {{#tag:ref|{{Ethnologue{{!}}{{{iso3}}}{{!}}{{{name}}} }} |name=ethnologue16}}
  | {{{1}}}
  }}

returns the same output, except that it now does it twice: if the key value is input, then the 'if' statement is returned twice, and if anything else is input, the 'if' statement is returned once (as a ref) and the 'then' statement once (as a new field). Kwamikagami 05:08, 17 November 2011 (UTC)

I modified w:Template:Infobox language/ref to what you might want, but is seems w:Template:Infobox language2 needs some fixes.--Patrick (talk) 07:55, 17 November 2011 (UTC)
Thanks. That helps get around the tag problem, but it still returns a string rather than a variable. I have no idea why that is happening, or how to correct it. Kwamikagami 08:35, 17 November 2011 (UTC)
To pass on the value of {{{iso3}}} to an inner template, call the latter with iso3={{{iso3}}}. I made some provisional changes, you can fix the links in the refs, and/or remove one if only one is intended.--Patrick (talk) 07:39, 18 November 2011 (UTC)
Thanks, Patrick! I never would have figured that out. It works perfectly! Kwamikagami 15:45, 18 November 2011 (UTC)

I came across some bizarre behaviour you might be interested in. I recoded w:Template:Ethnologue with an if statement so that if there is no input, it would generate a generic reference. But if I use #ifexist: {{{1}}} (with or without a pipe in the {{{1}}}), then it depends on the value of {{{1}}}, which is unexpected. Here I tested various inputs. They're all fine now (with an #if: statement), but I have no idea why the behaviour of #ifexist would depend on the value in the field. Is it buggy, or is that just another feature that I'm clueless about? Kwamikagami 18:51, 18 November 2011 (UTC)

#ifexist:{{{1}}} checks the existence of the page with as name the value of {{{1}}}.--Patrick (talk) 23:04, 18 November 2011 (UTC)
Ah, thanks. I read that too quickly. Amazing what you can figure out if you actually read the instructions! Sorry for wasting your time. Kwamikagami 07:59, 19 November 2011 (UTC)
The other stuff, that you helped me with, is it covered somewhere? If not, I think it would be useful to add s.t. like it as an example on this page. S.t. like:
Modifying data with a call to template 'modify', after evaluation by template 'evaluate':
Call template2 from template1:
{{evaluate|{{{data1}}}|code={{{code}}}|name={{{name}}} }}
Evaluate {{{data1}}} as "{{{1}}}" on template 'evaluate', and where appropriate, pass data through template 'modify':
{{modify|{{{code}}}|{{{name}}} }}
For example, by generating a new value based on the other passed variables if it is a certain key value, and returning it as-is if it is not:
{{#ifeq: {{{1}}} | keyvalue | {{modify|{{{code}}}|{{{name}}} }} | {{{1}}} }}
Kwamikagami 08:31, 19 November 2011 (UTC)
Help:Template#A_parameter_value_depending_on_parameters explains passing on a parameter value to an inner template, with an example: "The most straightforward way of expressing a parameter value in terms of a parameter is using the same name and making it equal, e.g. {{Lan is|lr=af|le={{{le}}}}} in Template:Lan is mr."--Patrick (talk) 10:41, 20 November 2011 (UTC)

the #if function nestable[edit]

I wanted to add that nesting of the #if function is supported but wasn't clear where to add it. If someone has an idea, please go ahead.

The nesting would be in the third parameter. Example: {{#if: test string | value if test string is not empty | {{#if: test string | value if test string is not empty | value if test string is empty (or only white space) }} }}

Nesting can be to at least seven levels, possibly subject to a memory limit.

I assume other functions can be nested but I don't know which ones. I don't know the maximum depth of allowed nesting for the #if function or any other nestable function. If nesting is not subject to a memory limit, that statement should be deleted.

Nick Levinson (talk) 18:08, 24 August 2013 (UTC) (Corrected an opening nowiki tag: 18:13, 24 August 2013 (UTC))

This is for the general case more or less treated in Help:Expansion#Pipes: "This is applied recursively". For specific info on #if we had Help:ParserFunctions, but this was moved to mw:Help:Extension:ParserFunctions, with "The function may be nested." - Patrick (talk) 23:49, 24 August 2013 (UTC)
That first link is to a page I would not have understood clearly as meaning that nesting is allowed, so it needs rewording somehow, and the MediaWiki page is that way because I added the text (it wasn't moved), but since this page is separate and is a logical place for finding out how to use the function it should say, but the string "nest" isn't on it. Giving an example and the maximum depth would also help, unless there's a more general location for that, and if there is then that should be cross-referenced. Nick Levinson (talk) 15:54, 27 August 2013 (UTC)
We have Help:Expansion depth, but it is not always clear to me how the limit works. Since #if is not a template (except in the generalized sense of Help:Expansion) it does not particularly belong on a page about templates. The nesting is a general expansion issue, but examples on mw:Help:Extension:ParserFunctions are fine. - Patrick (talk) 23:30, 27 August 2013 (UTC)