Creating an account only takes 20 seconds, and doesn’t require any personal info.

If you’ve got one already, please log in.🤝

Difference between revisions of “Help:Templates”

From Teflpedia
m (Text replacement - "e's" to "e’s")
 
(27 intermediate revisions by 3 users not shown)
Line 7: Line 7:
For example, <tt><nowiki>{{:Michael Lewis}}</nowiki></tt> will transclude an exact copy of [[:Michael Lewis|that page]] (when you alter the page, anywhere that transcludes it will also display the change) like so (without the lines above and below which have been added on this page) Note that in this example a blank space is left where <tt>namespace</tt> should appear because it is in "Main" namespace.:
For example, <tt><nowiki>{{:Michael Lewis}}</nowiki></tt> will transclude an exact copy of [[:Michael Lewis|that page]] (when you alter the page, anywhere that transcludes it will also display the change) like so (without the lines above and below which have been added on this page) Note that in this example a blank space is left where <tt>namespace</tt> should appear because it is in "Main" namespace.:
<div style="border-top:double 5px;border-bottom:double 5px;">
<div style="border-top:double 5px;border-bottom:double 5px;">
{{:Michael Lewis}}
 
</div>
</div>
As you can see, when transcluding an article like the above it's as if the ''exact wikitext'' was pasted into where the template is - you can't tell what is and what isn't a template by looking at the final article. It even appears in this page's table of contents.
As you can see, when transcluding an article like the above it's as if the ''exact wikitext'' was pasted into where the template is - you can't tell what is and what isn't a template by looking at the final article. It even appears in this page’s table of contents.




Line 35: Line 35:
===Restricted characters===
===Restricted characters===


Restricted characters are those that have special meaning within MediaWiki's template syntax, such as:
Restricted characters are those that have special meaning within MediaWiki’s template syntax, such as:


:= (used to assign parameter names)
:= (used to assign parameter names)
Line 50: Line 50:
==Selective transclusion==
==Selective transclusion==


In order to transclude part of a page only, sections of a template can be blocked out with special <tt><nowiki><noinclude> </noinclude></nowiki></tt> tags. When placed around a block of text, <tt><nowiki><noinclude>like so<noinclude></nowiki></tt>, the text will be exempt from transclusion. This is often used for explanations of how to use the template. It was used in the example (<tt><nowiki>{{:Michael Lewis}}</nowiki></tt>) at the top of the page to avoid carrying a "stub" template and a category over.
In order to transclude part of a page only, sections of a template can be blocked out with special '''<nowiki><noinclude></nowiki>''' tags. When placed around a block of text, <tt><nowiki><noinclude>like so<noinclude></nowiki></tt>, the text will be exempt from transclusion. This is often used for explanations of how to use the template. It was used in the example (<tt><nowiki>{{:Michael Lewis}}</nowiki></tt>) at the top of the page to avoid carrying a "stub" template and a category over.


Content can be made to show up ''only'' in transclusions by using <tt><nowiki><includeonly></nowiki></tt> tags. This might be used to put the receiving page into a particular category without putting the template itself into that category: <tt><nowiki><includeonly>[[category:Greek]]</includeonly></nowiki></tt> will put the receiving page into the (non-existent) category <tt>Greek</tt>.
Content can be made to show up ''only'' in transclusions by using '''<nowiki><includeonly></nowiki>''' tags. This might be used to put the receiving page into a particular category without putting the template itself into that category: <tt><nowiki><includeonly>[[category:greek]]</includeonly></nowiki></tt> will put the receiving page into the (non-existent) category <tt>Greek</tt>.


The last option is the <tt><nowiki><onlyinclude></nowiki></tt> tag. This allows the material to appear in the source, but limits transclusion to that section.
The last option is the '''<nowiki><onlyinclude></nowiki>''' tag. If this tag is used, only the content it encloses will be transcluded but it will also show on the Template page itself.


==Making a template==
==Making a template==
Line 60: Line 60:
Templates are made like any other Media Wiki page but are put in the template namespace. The easiest way to learn about templates is to copy and alter an existing one - testing the results on your user page or in a sandbox before placing it all over the site, of course. This can be easily done with the many "userbox" templates that adorn many userpages.
Templates are made like any other Media Wiki page but are put in the template namespace. The easiest way to learn about templates is to copy and alter an existing one - testing the results on your user page or in a sandbox before placing it all over the site, of course. This can be easily done with the many "userbox" templates that adorn many userpages.


'''<nowiki><noinclude></nowiki>''' Text between ''noinclude'' tags on a template wont be transcludeded--it will only show on the Template page itself. Put a note about how to ==Use== the template on the template's page within these noinclude tags.<nowiki></noinclude></nowiki>  
===<nowiki><noinclude></nowiki>===
Text between '''noinclude''' tags on a template '''wont be transcludeded'''--it will only show on the template page itself. Use this to put a note about how to use the template on the template page itself. Example:
 
<pre>
<noinclude>
==Use==
Say what the template is for, and show how to put it on a page  
(typically we show wiki markup in &lt;pre> tags so that it looks
like an example and doesn't get parsed).
 
&lt;pre>
{{Name of template here}}
&lt;/pre>
</noinclude>
</pre>
 
===<nowiki><includeonly></nowiki>===
Text between '''includeonly''' tags on a template '''will only be transcludeded'''--it will not show on the Template page itself. If you want all pages with the template on it to be listed in a particular [[help:categories|categories]] (but you don't want the Template page listed in the category) then put a category tag in your Template within ''includeonly'' tags.


'''<nowiki><includeonly></nowiki>''' Text between ''includeonly'' tags on a template will only be transcludeded--it will not show on the Template page itself. If you want all pages with the template on it to be listed in a particular category (but you don't want the Template page listed in the category) then put a category tag in your Template within ''includeonly'' tags<nowiki></includeonly></nowiki>  
<pre>
<includeonly>
[[category:short articles]]
</includeonly>
</pre>


An basic Template example that does all of the above is [[Template:Reference needed]]. Click on it's Edit tab and have a look!
A basic template that uses noinclude and includeonly tags is [[Template:Reference needed]]. Click on its Edit tab and have a look!


==Complex templates==
==Complex templates==
Line 71: Line 92:


==Purge==
==Purge==
Sometimes a template that has been recently edited does not display as expected because MediaWiki's cache still contains the old version of the template. If you are experiencing this, '''purge''' the page to force MediaWiki to refresh its cache for that page.
Sometimes a template that has been recently edited does not display as expected because MediaWiki’s cache still contains the old version of the template. If you are experiencing this, '''purge''' the page to force MediaWiki to refresh its cache for that page.


A simple way to "purge" a MediaWiki page you are viewing in your browser is:
A simple way to "purge" a MediaWiki page you are viewing in your browser is:
:click its "View history" tab, then in your browser's location field delete just the word "history" off the end of the URL, type "purge" there (after the "=") on the end of the URL, and hit return.
:click its "View history" tab, then in your browser's location field delete just the word "history" off the end of the URL, type "purge" there (after the "=") on the end of the URL, and hit return.


This refreshes MediaWiki's cache for all templates used on that page.
This refreshes MediaWiki’s cache for all templates used on that page.


==See also==


[[Category:Help|Templates]]
[[category:Teflpedia help|Templates]]

Latest revision as of 17:50, 19 January 2022

A template is a page that is included in another page.

Transclusion in Media Wiki[edit source]

Any page can be used as a template, all that is necessary is to put its title between two pairs of braces: {{namespace:pagename}}. When viewing a page with another page included in it, the template will appear exactly where code is located in the wikitext. The official term used when a template is put into a page is "transclusion".

For example, {{:Michael Lewis}} will transclude an exact copy of that page (when you alter the page, anywhere that transcludes it will also display the change) like so (without the lines above and below which have been added on this page) Note that in this example a blank space is left where namespace should appear because it is in "Main" namespace.:

As you can see, when transcluding an article like the above it's as if the exact wikitext was pasted into where the template is - you can't tell what is and what isn't a template by looking at the final article. It even appears in this page’s table of contents.


Substitution[edit source]

Putting "subst:" after the double opening braces ({{subst:pagename}}) is called substitution. When the page is saved, the subst tag is replaced by the wikitext of the template, with the parameter values substituted for the parameters. N.B.: This means that the displayed content will not be changed if the template itself is amended.

Template space[edit source]

Pages that are designed to be readily and repeatedly used are usually in the Template namespace. In the case of the template space the namespace does not need to be included inside the braces: {{pagename}}. For example, {{Boxword‎ }} is very commonly used on Help articles and is responsible for generating this box.

Parameters[edit source]

There are complications. Templates can have "parameters" which will affect the appearance or content.

When using a template with unnamed variables care must be taken.
If using just (say) variables 1 and 4 then use it as:

{{templatename|foo|4=bar}}
or
{{templatename|1=foo|4=bar}},
or
{{templatename|foo|||bar}}
(see template:Cquote for an occasion when you will probably need to do this).

It will also be necessary to include "number=" if there are any equals signs (=) in the variable.

If or when you create a new template, especially if it's more than a simple transclusion, please include instructions on the template page itself. Render them safe using the <noinclude> </noinclude> tags around the instructions so they aren't transcluded.

Restricted characters[edit source]

Restricted characters are those that have special meaning within MediaWiki’s template syntax, such as:

= (used to assign parameter names)
|   (used to separate parameters)


To use a restricted character within a template parameter, transclude it from another template! Restricted characters are not restricted in templates, just in parameters.

Examples, in template parameter calls:

  • For "=" use {{=}} -- Template:= contains "="
  • For "|" use {{!}} -- Template:! contains "|" (Note that we use ! in this template name instead of | because | cannot be part of a page name.)
  • For "||" use {{!!}} -- Template:!! contains "||"

Selective transclusion[edit source]

In order to transclude part of a page only, sections of a template can be blocked out with special <noinclude> tags. When placed around a block of text, <noinclude>like so<noinclude>, the text will be exempt from transclusion. This is often used for explanations of how to use the template. It was used in the example ({{:Michael Lewis}}) at the top of the page to avoid carrying a "stub" template and a category over.

Content can be made to show up only in transclusions by using <includeonly> tags. This might be used to put the receiving page into a particular category without putting the template itself into that category: <includeonly>[[category:greek]]</includeonly> will put the receiving page into the (non-existent) category Greek.

The last option is the <onlyinclude> tag. If this tag is used, only the content it encloses will be transcluded but it will also show on the Template page itself.

Making a template[edit source]

Templates are made like any other Media Wiki page but are put in the template namespace. The easiest way to learn about templates is to copy and alter an existing one - testing the results on your user page or in a sandbox before placing it all over the site, of course. This can be easily done with the many "userbox" templates that adorn many userpages.

<noinclude>[edit source]

Text between noinclude tags on a template wont be transcludeded--it will only show on the template page itself. Use this to put a note about how to use the template on the template page itself. Example:

<noinclude>
==Use==
Say what the template is for, and show how to put it on a page 
(typically we show wiki markup in <pre> tags so that it looks 
like an example and doesn't get parsed).

<pre>
{{Name of template here}}
</pre>
</noinclude>

<includeonly>[edit source]

Text between includeonly tags on a template will only be transcludeded--it will not show on the Template page itself. If you want all pages with the template on it to be listed in a particular categories (but you don't want the Template page listed in the category) then put a category tag in your Template within includeonly tags.

<includeonly>
[[category:short articles]]
</includeonly>

A basic template that uses noinclude and includeonly tags is Template:Reference needed. Click on its Edit tab and have a look!

Complex templates[edit source]

Templates can be extremely complex, allowing the use of many variables and "sub" templates. If you really want to know about them try here at meta Wikimedia. The majority of the situations where a complicated and multilayered template may be needed (such as those that make up the mainpage), templates are already in existence.

Purge[edit source]

Sometimes a template that has been recently edited does not display as expected because MediaWiki’s cache still contains the old version of the template. If you are experiencing this, purge the page to force MediaWiki to refresh its cache for that page.

A simple way to "purge" a MediaWiki page you are viewing in your browser is:

click its "View history" tab, then in your browser's location field delete just the word "history" off the end of the URL, type "purge" there (after the "=") on the end of the URL, and hit return.

This refreshes MediaWiki’s cache for all templates used on that page.