Template:When on basepage/doc: Difference between revisions
meta>Joe Decker m Reverted edits by 208.54.83.194 (talk) to last version by Billy545455 |
meta>Wikid77 +new section "Alternatives for simpler text" for how a subpage can be detected without using a template |
||
Line 1: | Line 1: | ||
{{documentation subpage}} | {{documentation subpage}} | ||
{{high-risk| | {{high-risk| 410,000+ }} | ||
<!-- PLEASE ADD CATEGORIES AND INTERWIKIS AT THE BOTTOM OF THIS PAGE --> | <!-- PLEASE ADD CATEGORIES AND INTERWIKIS AT THE BOTTOM OF THIS PAGE --> | ||
This is the {{tl|basepage subpage}} meta-template. | This is the {{tl|basepage subpage}} meta-template. | ||
This template helps other templates detect if they are on a '''basepage''', '''subpage''' or '''subsubpage'''. | This template helps other templates detect if they are on a '''basepage''', '''subpage''' or '''subsubpage'''. However, a subpage can be detected without using another template, by instead using a single <code>#ifeq</code> with parser function <code>#titleparts</code> (see below: [[#Alternatives for simpler text|Alternatives for simpler text]]). | ||
=== Usage === | === Usage === | ||
Line 121: | Line 121: | ||
This template detects subpages even when used in namespaces that doesn't have the MediaWiki [[meta:Link#Subpage feature|subpage feature]] enabled. Thus this template works the same in all namespaces. | This template detects subpages even when used in namespaces that doesn't have the MediaWiki [[meta:Link#Subpage feature|subpage feature]] enabled. Thus this template works the same in all namespaces. | ||
=== Alternatives for simpler text === | |||
A subpage can be detected without using another template, by instead using a single <code>#ifeq</code> along with the title-splitting parser function <code>#titleparts</code>, with expansion depth +1, as follows: | |||
::: <nowiki>{{#ifeq: {{#titleparts:{{PAGENAME}}|1|1}} | {{PAGENAME}}</nowiki> | |||
:::: <nowiki> | ...then on base page...</nowiki> | |||
:::: <nowiki> | ...else on a subpage...</nowiki> | |||
::: <nowiki>}}</nowiki> | |||
For example, many templates can check if the page is a /sandbox version, by the similar markup with <code>#ifeq</code> and <code>#titleparts</code>: | |||
::: <nowiki>{{#ifeq: {{#titleparts:{{PAGENAME}}|1|1}} | {{PAGENAME}}</nowiki> | |||
:::: <nowiki> | {{documentation}}</nowiki> | |||
:::: <nowiki> | {{template sandbox notice}}</nowiki> | |||
::: <nowiki>}}</nowiki> | |||
That simple logic works because [[Template:Template_sandbox_notice]] also checks the page name for "/sandbox" before displaying the sandbox notice; otherwise, any other subpage would show no extra text. By avoiding a template, and using a single <code>#ifeq</code>, then the processing uses only +2 expansion depth levels, rather than +7 levels, and avoids "[[wp:exceeded template limits]]" inside a complex /doc page. | |||
=== See also === | === See also === |
Revision as of 15:47, 20 November 2012
This is a documentation subpage for Template:When on basepage. It may contain usage information, categories and other content that is not part of the original template page. |
This template is used on 410,000+ pages, or roughly 6073% of all pages. To avoid major disruption and server load, any changes should be tested in the template's /sandbox or /testcases subpages, or in your own user subpage. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
This is the {{basepage subpage}} meta-template.
This template helps other templates detect if they are on a basepage, subpage or subsubpage. However, a subpage can be detected without using another template, by instead using a single #ifeq
with parser function #titleparts
(see below: Alternatives for simpler text).
Usage
This template takes one or more parameters, like this:
{{basepage subpage | Basepage text | Subpage text }}
If the template is on the page "User:Example", it shall return this:
- Basepage text
If the template is on "User:Example/test" or "User:Example/test/test" or any page lower than that, it will return this:
- Subpage text
This template can also detect "subsubpages", like this:
{{basepage subpage | Basepage text | Subpage text | Subsubpage text }}
If the template is on "User:Example/test/test" or any page lower than that, it will return this:
- Subsubpage text
By using an empty parameter you can make it so the template doesn't render anything for some specific page type. Like this:
{{basepage subpage | Basepage text | Subpage text | }}
The code above will render nothing when on "User:Example/test/test" or lower, but will return this when on "User:Example/test":
- Subpage text
The "page" parameter
For testing and demonstration purposes this template can take a parameter named page. Like this:
{{basepage subpage | Basepage text | Subpage text | page = User:Example/test }}
No matter on what kind of page the code above is used it will return this:
- Subpage text
The page parameter makes this template behave exactly as if on that page. Thus, if a subsubpagename like "User:Example/test/test" is fed, then it returns the subsubpage text if there is one, otherwise it returns the subpage text.
The pagename doesn't have to be an existing page.
If the parameter is empty or undefined, the name of the current page determines the result.
You can make it so your template also understands the page parameter. That means you can demonstrate the different appearances of your template in the documentation for your template. Then do like this:
{{basepage subpage | Basepage text | Subpage text | page = {{{page|}}} }}
Technical details
Templates have a problem to handle parameter data that contains equal signs "=
". But that is easily solved by using numbered parameters. Like this:
{{basepage subpage | 1 = Basepage text | 2 = Subpage text | 3 = Subsubpage text | page = {{{page|}}} }}
This template detects subpages even when used in namespaces that doesn't have the MediaWiki subpage feature enabled. Thus this template works the same in all namespaces.
Alternatives for simpler text
A subpage can be detected without using another template, by instead using a single #ifeq
along with the title-splitting parser function #titleparts
, with expansion depth +1, as follows:
- {{#ifeq: {{#titleparts:{{PAGENAME}}|1|1}} | {{PAGENAME}}
- | ...then on base page...
- | ...else on a subpage...
- }}
- {{#ifeq: {{#titleparts:{{PAGENAME}}|1|1}} | {{PAGENAME}}
For example, many templates can check if the page is a /sandbox version, by the similar markup with #ifeq
and #titleparts
:
- {{#ifeq: {{#titleparts:{{PAGENAME}}|1|1}} | {{PAGENAME}}
- | {{documentation}}
- | {{template sandbox notice}}
- }}
- {{#ifeq: {{#titleparts:{{PAGENAME}}|1|1}} | {{PAGENAME}}
That simple logic works because Template:Template_sandbox_notice also checks the page name for "/sandbox" before displaying the sandbox notice; otherwise, any other subpage would show no extra text. By avoiding a template, and using a single #ifeq
, then the processing uses only +2 expansion depth levels, rather than +7 levels, and avoids "wp:exceeded template limits" inside a complex /doc page.
See also
Template:Pagename detect see also