Template:When on basepage/doc: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Sardanaphalus
m Sardanaphalus moved page Template:Basepage subpage/doc to Template:When on basepage/doc: Per template page
meta>Sardanaphalus
updates following move
Line 1: Line 1:
{{documentation subpage}}
{{Documentation subpage}}
<!--Categories where indicated at the bottom of this page, please; interwikis at Wikidata (see [[Wikipedia:Wikidata]])-->
<!--Categories where indicated at the bottom of this page, please; interwikis at Wikidata (see [[Wikipedia:Wikidata]])-->


This is the {{tl|basepage subpage}} meta-template.
This is the {{tl|When on basepage}} meta-template. It helps other templates detect if they are on a '''basepage''' (i.e. a page whose name matches the pattern "''x'':''x''") '''subpage''' ("''x'':''x''/''x''") or '''subsubpage''' ("''x'':''x''/''x''/''x''").


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]]).
An alternative way to detect whether a page is a subpage is to combine the parser functions {{plaincode|#ifeq:}} and {{plaincode|#titleparts}}. See [[#Alternatives for simpler text|Alternatives for simpler text]] below.


== Usage ==
== Usage ==
This template takes one or more parameters, like this:


<pre>
: {{tlx|When on basepage&#32;|''result if on a basepage''&#32;(|''result if on a subpage'')&#32;(|''result if on a subsubpage'')&#32;(|page{{=}}''pagename'')}}
{{basepage subpage
| Basepage text
| Subpage text
}}
</pre>
 
If the template is on the page "User:Example", it shall return this:
 
:{{basepage subpage
| Basepage text
| Subpage text
| page = User:Example
}}
 
If the template is on "User:Example/test" or "User:Example/test/test" or any page lower than that, it will return this:
 
:{{basepage subpage
| Basepage text
| Subpage text
| page = User:Example/test/test
}}


This template can also detect "subsubpages", like this:
For example, {{tcpl|When on basepage |Basepage |Subpage}} on the page "User:Example" would return: {{When on basepage|Basepage|Subpage|page=User:Example}}<br/>whilst on "User:Example/page" (or "User:Example/page/page", "User:Example/page/page/page", etc) it would return: {{When on basepage|Basepage|Subpage|page=User:Example/page}}


<pre>
On a subsubpage such as "User:Example/page/page" (or "User:Example/page/page/page", etc), {{nowrap|{{tcpl|When on basepage |Basepage |Subpage |Subsubpage}}}} would return: {{When on basepage |Basepage |Subpage |Subsubpage |page=User:Example/page/page}}
{{basepage subpage
| Basepage text
| Subpage text
| Subsubpage text
}}
</pre>


If the template is on "User:Example/test/test" or any page lower than that, it will return this:
Use an empty parameter to specify no action for a particular type of page. {{nowrap|{{tcpl|When on basepage |Basepage | |Subsubpage}}}} on "User:Example/page", for example, would return nothing, but would return "Subsubpage" if on "User:Example/page/page", "User:Example/page/page/page", etc (and "Basepage" if on "User:Example").


:{{basepage subpage
===Handling equals-signs ("=")===
| Basepage text
Use numbered parameters if their values include equals-signs ("="), i.e.
| Subpage text
: {{tcpl|When on basepage&#32;|1{{=}}''basepage result'' (|2{{=}}''subpage result'') (|3{{=}}''subsubpage result'')}}
| Subsubpage text
| page = User:Example/test/test
}}


By using an empty parameter you can make it so the template doesn't render anything for some specific page type. Like this:
===''page'' parameter===
The ''page'' parameter is included for testing/demonstration. For example:
: {{tcpl|When on basepage&#32;|(basepage result) |(subpage result) |page{{=}}User:Example/test}}
Regardless of where the above is placed, it will return: {{When on basepage|(basepage result)|(subpage result)|page=User:Example/test}}<br/>(because "User:Example/test" is a subpage). The page doesn't have to exist.


<pre>
If ''page'' is left empty ({{para|page}}) or undefined, the page where the template is transcluded (i.e. {{wikivar|PAGENAME}}) is used.
{{basepage subpage
| Basepage text
| Subpage text
|  
}}
</pre>


The code above will render nothing when on "User:Example/test/test" or lower, but will return this when on "User:Example/test":
''page'' also accepts variables, e.g. {{nowrap|{{tcpl|When on basepage |(basepage result) |(subpage result) |page<nowiki>={{{page|}}}</nowiki>&nbsp;}}}}


:{{basepage subpage
== Technical note ==
| Basepage text
| Subpage text
|
| page = User:Example/test
}}


=== The "page" parameter ===
This template detects subpages in all namespaces, including those without the [[meta:Link#Subpage feature|MediaWiki subpage feature]] enabled.
For testing and demonstration purposes this template can take a parameter named '''page'''. Like this:


<pre>
== Alternatives for simpler text ==
{{basepage subpage
| Basepage text
| Subpage text
| page = User:Example/test
}}
</pre>
 
No matter on what kind of page the code above is used it will return this:
 
:{{basepage subpage
| Basepage text
| Subpage text
| page = User:Example/test
}}
 
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:


A subpage can be detected without using another template, by instead using the title-splitting parser function {{plaincode|#titleparts}} with {{plaincode|#ifeq}}:
<pre>
<pre>
{{basepage subpage
{{#ifeq:{{#titleparts:{{PAGENAME}}|1|1}}|{{PAGENAME}}
| Basepage text
| [...code if on a basepage...]
| Subpage text
| [...code if on a subpage...]
| page = {{{page|}}}
}}
}}
</pre>
</pre>
== Technical details ==
Templates have a problem to handle parameter data that contains equal signs "<code>=</code>". But that is easily solved by using numbered parameters. Like this:
<pre>
{{basepage subpage
| 1 = Basepage text
| 2 = Subpage text
| 3 = Subsubpage text
| page = {{{page|}}}
}}
</pre>
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>:
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>{{#ifeq: {{#titleparts:{{PAGENAME}}|1|1}} | {{PAGENAME}}</nowiki>
:::: <nowiki> | {{documentation}}</nowiki>
:: <nowiki> | {{documentation}}</nowiki>
:::: <nowiki> | {{template sandbox notice}}</nowiki>
:: <nowiki> | {{template sandbox notice}}</nowiki>
::: <nowiki>}}</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.
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 ==
{{pagename detect see also}}
{{Pagename detect see also}}


<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox | |
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox |
<!--Categories below this line, please; interwikis at Wikidata-->
| <!--Categories below this line, please; interwikis at Wikidata-->
[[Category:Namespace manipulation templates]]
[[Category:Namespace manipulation templates]]
[[Category:Wikipedia metatemplates]]
[[Category:Wikipedia metatemplates]]
}}</includeonly>
}}</includeonly>

Revision as of 10:13, 30 September 2014

This is the {{When on basepage}} meta-template. It helps other templates detect if they are on a basepage (i.e. a page whose name matches the pattern "x:x") subpage ("x:x/x") or subsubpage ("x:x/x/x").

An alternative way to detect whether a page is a subpage is to combine the parser functions Template:Plaincode and Template:Plaincode. See Alternatives for simpler text below.

Usage

{{When on basepage |result if on a basepage (|result if on a subpage) (|result if on a subsubpage) (|page=pagename)}}

For example, Template:Tcpl on the page "User:Example" would return: Basepage
whilst on "User:Example/page" (or "User:Example/page/page", "User:Example/page/page/page", etc) it would return: Subpage

On a subsubpage such as "User:Example/page/page" (or "User:Example/page/page/page", etc), Template:Tcpl would return: Subsubpage

Use an empty parameter to specify no action for a particular type of page. Template:Tcpl on "User:Example/page", for example, would return nothing, but would return "Subsubpage" if on "User:Example/page/page", "User:Example/page/page/page", etc (and "Basepage" if on "User:Example").

Handling equals-signs ("=")

Use numbered parameters if their values include equals-signs ("="), i.e.

Template:Tcpl

page parameter

The page parameter is included for testing/demonstration. For example:

Template:Tcpl

Regardless of where the above is placed, it will return: (subpage result)
(because "User:Example/test" is a subpage). The page doesn't have to exist.

If page is left empty (|page=) or undefined, the page where the template is transcluded (i.e. Template:Wikivar) is used.

page also accepts variables, e.g. Template:Tcpl

Technical note

This template detects subpages in all namespaces, including those without the MediaWiki subpage feature enabled.

Alternatives for simpler text

A subpage can be detected without using another template, by instead using the title-splitting parser function Template:Plaincode with Template:Plaincode:

{{#ifeq:{{#titleparts:{{PAGENAME}}|1|1}}|{{PAGENAME}}
 | [...code if on a basepage...]
 | [...code if on a subpage...]
}}

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}}
}}

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