User:Gorgon/Sandbox/Sandbox Charlie: Difference between revisions

From Avlis Wiki
Jump to navigation Jump to search
(Test version of Template:ns detect (Namespace detect).)
 
m (Updated description.)
Line 2: Line 2:
   {{lc:              <!--Lower case the result-->
   {{lc:              <!--Lower case the result-->
     <!--If no or empty "demospace" parameter then detect namespace-->
     <!--If no or empty "demospace" parameter then detect namespace-->
     {{#if:{{{demospace|}}}  
     {{#if:{{{demospace|}}}
     | {{{demospace}}}
     | {{{demospace}}}
     | {{#if:{{{page|}}}  
     | {{#if:{{{page|}}}
       | <!--Detect the namespace in the "page" parameter-->
       | <!--Detect the namespace in the "page" parameter-->
         {{#ifeq:{{NAMESPACE:{{{page}}} }}|{{TALKSPACE:{{{page}}} }}
         {{#ifeq:{{NAMESPACE:{{{page}}} }}|{{TALKSPACE:{{{page}}} }}
         | talk
         | talk
         | {{SUBJECTSPACE:{{{page}}} }}  
         | {{SUBJECTSPACE:{{{page}}} }}
         }}
         }}
       | <!--No "demospace" or "page" parameters, so detect actual namespace-->
       | <!--No "demospace" or "page" parameters, so detect actual namespace-->
         {{#ifeq:{{NAMESPACE}}|{{TALKSPACE}}
         {{#ifeq:{{NAMESPACE}}|{{TALKSPACE}}
         | talk
         | talk
         | {{SUBJECTSPACE}}  
         | {{SUBJECTSPACE}}
         }}
         }}
       }}
       }}
Line 69: Line 69:




This template takes one or more parameters, named after the different namespaces listed above. Like this:
This template takes one or more parameters, named after the different namespace types listed above. Like this:


<pre style="white-space: pre">
<pre style="white-space: pre">
Line 115: Line 115:




By using an empty parameter, you can make it so the template doesn't render anything for a specific namespace page type. Like this:
By using an empty parameter, you can make it so the template doesn't render anything for a specific namespace type page. Like this:


<pre style="white-space: pre">
<pre style="white-space: pre">
{{ns detect
{{ns detect
| main  =  
| main  =
| other = Other pages text
| other = Other pages text
}}
}}
</pre>
</pre>


The code above will render nothing when in main (article) namespace pages, but will return this when in other namespace pages:
The code above will render nothing when in main (article) namespace pages, but will return this when in other namespace type pages:


:{{<!-- ns detect -->User:Gorgon/Sandbox/Sandbox Charlie
:{{<!-- ns detect -->User:Gorgon/Sandbox/Sandbox Charlie
| main  =  
| main  =
| other = Other pages text
| other = Other pages text
}}
As an extended example of empty parameter usage, if you need to assign the same value to more than one namespace type parameter as a result, and the value is a larger block of code, it is inefficient to duplicate the same code for multiple parameter values. Instead, you can use empty parameters to return nothing for namespace type pages that you don't want the value used in (all except the ones you need), and then use the '''other''' parameter value to return the result of your block of code for the namespace type pages that you do want it in. Like this:
<pre style="white-space: pre">
{{ns detect
| talk = | avlis wiki = | file = | mediawiki = | help = | category =
| other = {{{|Result of large, multi-line block of code.}}}
}}
</pre>
For the result below, when in main (article), user and template namespace pages, the code above will use the '''other''' parameter value, since those 3 namespace types weren't included as parameters. When in all other namespace type pages, nothing is returned, since they all have empty values:
:{{<!-- ns detect -->User:Gorgon/Sandbox/Sandbox Charlie
| talk = | avlis wiki = | file = | mediawiki = | help = | category =
| other = {{{|Result of large, multi-line block of code.}}}
}}
}}


Line 134: Line 151:
=== Demospace and page ===
=== Demospace and page ===


For testing and demonstration purposes, this template can take two parameters, named '''demospace''' and '''page'''.  
For testing and demonstration purposes, this template can take two parameters, named '''demospace''' and '''page'''. Only assign a value to one of these in your template. If both have values assigned, then only the '''demospace''' parameter will be used.


'''Demospace''' understands any of the namespace page type names used by this template, including the '''other''' namespace type. It tells the template to behave like it is in a specific type of namespace page. Like this:
'''Demospace''' understands any of the namespace type names used by this template, including the '''other''' namespace type. It tells the template to behave like it is in a specific namespace type page. Like this:


<pre style="white-space: pre">
<pre style="white-space: pre">
{{ns detect  
{{ns detect
| main      = Article text
| main      = Article text
| other    = Other pages text
| other    = Other pages text
Line 146: Line 163:
</pre>
</pre>


No matter what kind of namespace page the code above is used in, it will return this:
No matter what namespace the code above is used in, it will return this:


:{{<!-- ns detect -->User:Gorgon/Sandbox/Sandbox Charlie  
:{{<!-- ns detect -->User:Gorgon/Sandbox/Sandbox Charlie
| main      = Article text
| main      = Article text
| other    = Other pages text
| other    = Other pages text
Line 155: Line 172:




The '''page''' parameter instead takes a normal <span class="plainlinks">[http://www.mediawiki.org/wiki/Help:Magic_words#Page_names &#123;&#123;PAGENAME&#125;&#125;]</span> (Full page title, including all subpage levels, without the namespace). It makes this template behave exactly as if rendered on that page. The pagename doesn't have to be an existing page. Like this:
The '''page''' parameter instead takes a "namespace:pagename" value. It makes this template behave as if rendered in that page's namespace type. The page name doesn't have to be an existing page. Like this:


<pre style="white-space: pre">
<pre style="white-space: pre">
{{ns detect  
{{ns detect
| user  = User page text
| user  = User page text
| other = Other pages text
| other = Other pages text
Line 165: Line 182:
</pre>
</pre>


No matter what kind of namespace page the code above is used in, it will return this:
No matter what namespace the code above is used in, it will return this:


:{{<!-- ns detect -->User:Gorgon/Sandbox/Sandbox Charlie  
:{{<!-- ns detect -->User:Gorgon/Sandbox/Sandbox Charlie
| user  = User page text
| user  = User page text
| other = Other pages text
| other = Other pages text
Line 173: Line 190:
}}
}}


'''Note:''' The value of '''page''' is only used to determine the namespace type of that page. The "pagename" part of the "namespace:pagename" value isn't used for anything, but is required by the '''<nowiki>{{NAMESPACE}}</nowiki>''', '''<nowiki>{{TALKSPACE}}</nowiki>''' and '''<nowiki>{{SUBJECTSPACE}}</nowiki>''' magic words that are used by the '''<nowiki>{{ns detect}}</nowiki>''' template (see: [http://www.mediawiki.org/wiki/Help:Magic_words#Namespaces Help:Magic words#Namespaces]). The '''demospace''' parameter is usually a better choice for setting an alternate namespace type in testing, unless you need to use a "namespace:pagename" value.


It may be convenient to define the '''demospace''' and/or '''page''' parameters in your template, and send them on to the '''<nowiki>{{ns detect}}</nowiki>''' template. Then do like this:
 
It may be convenient to define the '''demospace''' and/or '''page''' parameters as (optional) named parameters by the page using your template, and send them on to the '''<nowiki>{{ns detect}}</nowiki>''' template.
 
E.g., A sandbox page in a userspace <code>User:Example/sandbox</code> for testing your template '''<nowiki>{{mytemplate}}</nowiki>''' as if it was in the main namespace.
 
'''Code in User:Example/sandbox:''' <code><nowiki>{{mytemplate | demospace = main}}</nowiki></code> or <code><nowiki>{{mytemplate | page = Example page}}</nowiki></code> '''Note:''' There is no "Main:" prefix on pages in the main namespace, so only the <code>Example page</code> "pagename" is required - This also allows testing other namespaces types from the same sandbox page by changing the parameter values, even using preview mode.
 
Then in your '''<nowiki>{{mytemplate}}</nowiki>''' template, do like this:


<pre style="white-space: pre">
<pre style="white-space: pre">
{{ns detect  
{{ns detect
| main      = Article text
| main      = Article text
| other    = Other pages text
| other    = Other pages text
Line 185: Line 210:
</pre>
</pre>


If both the '''demospace''' and '''page''' parameters are empty, or undefined, then the template will detect namespace page types as usual.
These parameters will be used by the template only if defined by the page using your template. If both the '''demospace''' and '''page''' parameters are empty, or undefined, then the template will detect namespace types as usual for the page. As described above, if both the '''demospace''' and '''page''' parameters have values assigned, then only the '''demospace''' parameter will be used.




Line 204: Line 229:
| category  =
| category  =
| other      =
| other      =
| demospace  = {{{demospace|}}}  
| demospace  = {{{demospace|}}}
               One of: main, talk, user, avlis wiki,
               One of: main, talk, user, avlis wiki,
                       file, mediawiki, template,  
                       file, mediawiki, template,
                       help, category, other
                       help, category, other
| page      = {{{page|}}} E.g., User:Example
| page      = {{{page|}}} E.g., User:Example
Line 212: Line 237:
</pre>
</pre>


'''Note:''' Empty values for the '''main''' through '''category''' parameters are different than not including (defining) them. An undefined parameter (i.e, one you do not include in your template usage) will use the result of the '''other''' parameter when in that namespace. Any that are defined, but not assigned a value (i.e, included in the template usage, but nothing after the = ) will render nothing when used in that namespace.
'''Note:''' Empty values for the '''main''' through '''category''' parameters are different than not including (defining) them. An undefined parameter (i.e, one you do not include in your template usage) will use the result of the '''other''' parameter when in that namespace type. Any that are defined, but not assigned a value (i.e, included in the template usage, but nothing after the = ) will render nothing when used in that namespace type.




Line 221: Line 246:
<span class="plainlinks">[http://www.mediawiki.org/wiki/Help:Templates Templates]</span> have a problem handling parameter data that contains pipes "<code>|</code>", unless the pipe is inside another template <code><nowiki>{{name|param1}}</nowiki></code>, or inside a piped link <code><nowiki>[[Help:Template|help]]</nowiki></code>. Therefore templates can not handle wikitables as input, unless you escape them by using the '''<nowiki>{{!}}</nowiki>''' magic word (parser function - see: <span class="plainlinks">[http://www.mediawiki.org/wiki/Help:Magic_words#Other Help:Magic words#Other]</span>). This makes it hard to use wikitables as parameters in templates. Instead, the usual solution is to use [[Wikipedia:Help:HTML_in_wikitext | HTML wikimarkup]] for the table code, which is more robust.
<span class="plainlinks">[http://www.mediawiki.org/wiki/Help:Templates Templates]</span> have a problem handling parameter data that contains pipes "<code>|</code>", unless the pipe is inside another template <code><nowiki>{{name|param1}}</nowiki></code>, or inside a piped link <code><nowiki>[[Help:Template|help]]</nowiki></code>. Therefore templates can not handle wikitables as input, unless you escape them by using the '''<nowiki>{{!}}</nowiki>''' magic word (parser function - see: <span class="plainlinks">[http://www.mediawiki.org/wiki/Help:Magic_words#Other Help:Magic words#Other]</span>). This makes it hard to use wikitables as parameters in templates. Instead, the usual solution is to use [[Wikipedia:Help:HTML_in_wikitext | HTML wikimarkup]] for the table code, which is more robust.


'''''Note:''' [[Template:!]] functionality was replaced in MediaWiki version 1.24 by the '''<nowiki>{{!}}</nowiki>''' magic word, and is ignored when using it.''
'''''Note:''' [[Template:!]] functionality was replaced in MediaWiki version 1.24 by the '''<nowiki>{{!}}</nowiki>''' magic word, and the template is ignored when using it.''


<!-- [[Category:Templates|{{PAGENAME}}]] --></noinclude>
<!-- [[Category:Templates|{{PAGENAME}}]] --></noinclude>

Revision as of 06:42, 1 February 2017


Template:Ns detect (Namespace detect)

This template is based on Template:Namespace detect.

It uses the original markup, modified for Avlis wiki namespaces. The documentation below was copied to this template, since we do not use documentation sub-pages. It has also been modified for Avlis namespaces, and most of the MediaWiki specific information, links and sub-templates have either been removed, or modified for the Avlis wiki. It has been reformatted, and several sections modified for improved clarity.

See the original template documentation for further details.


Template documentation

This is the {{ns detect}} (Namespace detect) meta-template.

It helps other templates detect what type of namespace they are used in.

It detects and groups all of the different namespaces used on the Avlis wiki into several types:

main = Main/article namespace, as in normal Avlis wiki articles.
talk = Any talk namespace, such as page names that start with "Talk:", "User talk:", "File talk:" and so on.
user, avlis wiki, file, mediawiki, template, help, and category = The other namespaces, except for talk namespace pages.
other = Any namespaces that were not specified as a parameter to the template. See the Note: in the " Parameters" section below.

For backwards compatibility, this template also understands the old name image for file. But using image is now deprecated.


Usage

Note: The following examples use multiple lines for improved readability. All of them can be written as single lines as well.

E.g., {{ns detect | main = Article text | talk = Talk page text | other = Other pages text}} for the first example below.


This template takes one or more parameters, named after the different namespace types listed above. Like this:

{{ns detect
| main  = Article text
| talk  = Talk page text
| other = Other pages text
}}

If the template is in a main (article) namespace page, it will return this:

Article text

If the template is used in any other namespace page than a main (article), or a talk page, it will return this:

Other pages text


The example above made the template return something for all other namespace page types. But if we don't use the other parameter, or leave it empty, then it will not return anything for the other namespace page types. Like this:

{{ns detect
| file     = File page text
| category = Category page text
| other    =
}}

In any pages, other than file and category namespace pages, the code above will render nothing.


By using an empty parameter, you can make it so the template doesn't render anything for a specific namespace type page. Like this:

{{ns detect
| main  =
| other = Other pages text
}}

The code above will render nothing when in main (article) namespace pages, but will return this when in other namespace type pages:

Other pages text


As an extended example of empty parameter usage, if you need to assign the same value to more than one namespace type parameter as a result, and the value is a larger block of code, it is inefficient to duplicate the same code for multiple parameter values. Instead, you can use empty parameters to return nothing for namespace type pages that you don't want the value used in (all except the ones you need), and then use the other parameter value to return the result of your block of code for the namespace type pages that you do want it in. Like this:

{{ns detect
| talk = | avlis wiki = | file = | mediawiki = | help = | category =
| other = {{{|Result of large, multi-line block of code.}}}
}}

For the result below, when in main (article), user and template namespace pages, the code above will use the other parameter value, since those 3 namespace types weren't included as parameters. When in all other namespace type pages, nothing is returned, since they all have empty values:

Result of large, multi-line block of code.


Demospace and page

For testing and demonstration purposes, this template can take two parameters, named demospace and page. Only assign a value to one of these in your template. If both have values assigned, then only the demospace parameter will be used.

Demospace understands any of the namespace type names used by this template, including the other namespace type. It tells the template to behave like it is in a specific namespace type page. Like this:

{{ns detect
| main      = Article text
| other     = Other pages text
| demospace = main
}}

No matter what namespace the code above is used in, it will return this:

Article text


The page parameter instead takes a "namespace:pagename" value. It makes this template behave as if rendered in that page's namespace type. The page name doesn't have to be an existing page. Like this:

{{ns detect
| user  = User page text
| other = Other pages text
| page  = User:Example
}}

No matter what namespace the code above is used in, it will return this:

User page text

Note: The value of page is only used to determine the namespace type of that page. The "pagename" part of the "namespace:pagename" value isn't used for anything, but is required by the {{NAMESPACE}}, {{TALKSPACE}} and {{SUBJECTSPACE}} magic words that are used by the {{ns detect}} template (see: Help:Magic words#Namespaces). The demospace parameter is usually a better choice for setting an alternate namespace type in testing, unless you need to use a "namespace:pagename" value.


It may be convenient to define the demospace and/or page parameters as (optional) named parameters by the page using your template, and send them on to the {{ns detect}} template.

E.g., A sandbox page in a userspace User:Example/sandbox for testing your template {{mytemplate}} as if it was in the main namespace.

Code in User:Example/sandbox: {{mytemplate | demospace = main}} or {{mytemplate | page = Example page}} Note: There is no "Main:" prefix on pages in the main namespace, so only the Example page "pagename" is required - This also allows testing other namespaces types from the same sandbox page by changing the parameter values, even using preview mode.

Then in your {{mytemplate}} template, do like this:

{{ns detect
| main      = Article text
| other     = Other pages text
| demospace = {{{demospace|}}}
| page      = {{{page|}}}
}}

These parameters will be used by the template only if defined by the page using your template. If both the demospace and page parameters are empty, or undefined, then the template will detect namespace types as usual for the page. As described above, if both the demospace and page parameters have values assigned, then only the demospace parameter will be used.


Parameters

List of all parameters:

{{ns detect
| main       =
| talk       =
| user       =
| avlis wiki =
| file       =
| mediawiki  =
| template   =
| help       =
| category   =
| other      =
| demospace  = {{{demospace|}}}
               One of: main, talk, user, avlis wiki,
                       file, mediawiki, template,
                       help, category, other
| page       = {{{page|}}} E.g., User:Example
}}

Note: Empty values for the main through category parameters are different than not including (defining) them. An undefined parameter (i.e, one you do not include in your template usage) will use the result of the other parameter when in that namespace type. Any that are defined, but not assigned a value (i.e, included in the template usage, but nothing after the = ) will render nothing when used in that namespace type.


Technical details

If you intend to use wikitables as parameter values in this template, then you need to know this:

Templates have a problem handling parameter data that contains pipes "|", unless the pipe is inside another template {{name|param1}}, or inside a piped link [[Help:Template|help]]. Therefore templates can not handle wikitables as input, unless you escape them by using the {{!}} magic word (parser function - see: Help:Magic words#Other). This makes it hard to use wikitables as parameters in templates. Instead, the usual solution is to use HTML wikimarkup for the table code, which is more robust.

Note: Template:! functionality was replaced in MediaWiki version 1.24 by the {{!}} magic word, and the template is ignored when using it.