Template:Ns detect

From Avlis Wiki
Jump to navigation Jump to search


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 and pages detect what type of namespace they are used in, and can return different results when used in them.

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.