Template:Namespace detect/doc: Difference between revisions

From Fallenchungus Wiki
Jump to navigation Jump to search
add all supported namespaces
m 1 revision imported: To get boxes working.
Line 1: Line 1:
{{Documentation subpage}}
{{Used in system}}
{{High-risk| approximately 140000 }}
{{Module rating|protected}}
{{Lua|Module:Namespace detect}}
{{Lua|Module:Namespace detect/data|Module:Yesno|Module:Arguments|Module:TableTools}}
<!--Categories where indicated at the bottom of this page, please; interwikis at Wikidata (see [[Wikipedia:Wikidata]])-->


This is the {{tl|Namespace detect}} meta-template.  It helps other templates detect what type of page they are on.
This module allows you to output different text depending on the [[WP:NAMESPACE|namespace]] that a given page is in. It is a [[WP:Lua|Lua]] implementation of the {{tl|namespace detect}} template, with a few improvements: all namespaces and all namespace aliases are supported, and namespace names are detected automatically for the local wiki.
 
It detects and groups all the different [[Wikipedia:Namespace#Enumeration|namespaces]] used on Wikipedia into several types:
 
; main : Main (i.e. article) space, where normal Wikipedia articles are kept.
; talk : Any talk space, including page names that start with "Talk:", "User talk:", "File talk:", etc.
'''{{hlist|portal|category|user|draft|wikipedia|mediawiki|book|timedtext|template|special|media|file|help|module}}'''
: The remaining namespaces.
; other : Any namespaces that were not specified as a parameter to the template (see explanation below).
 
For backwards compatibility, this template handles '''image''' as if '''file'''. '''image''' (<nowiki>[[Image:...]]</nowiki>) is now deprecated.
 
'''Note:''' For most cases it may be better to use the simpler namespace detection templates (see the [[#See also|see also]] section below). This template is more prone to human errors such as misspelling parameter names.


== Usage ==
== Usage ==


This template takes one or more parameters named after the different page types as listed above. Like this:
<pre>
<pre>
{{Namespace detect
{{#invoke: Namespace detect | main
| main = Article text
| page              = <!-- page to detect namespace for, if not the current page -->
| talk  = Talk page text
| main              = <!-- text to return for the main namespace -->
| other = Other pages text
| talk              = <!-- text to return for talk namespaces -->
}}
</pre>


If the template is on a main (article) page, it will return this:
<!-- text to return for specific subject namespaces -->
: {{Namespace detect |demospace=main
| portal            =
  | main  = Article text
| category          =
  | talk  = Talk page text
| user             =
  | other = Other pages text
| draft            =
  }}
| wikipedia        =
| mediawiki        =
| book              =
| timedtext        =
| template         =
| special          =  
| media            =  
| file              =  
| help             =  
| module            =


If the template is on any other page than an article or a talk page, it will return this:
| other             = <!-- text to return for unspecified namespaces -->
: {{Namespace detect
| demospace        = <!-- namespace to display text for -->
  | main  = Article text
  | talk  = Talk page text
  | other = Other pages text
  }}


The example above made the template return something for all page types. But if we don't use the '''other''' parameter or leave it empty, it will not return anything for the other page types. Like this:
| subjectns        = <!-- set to "yes" to treat talk pages as the corresponding subject page -->
<pre>
{{Namespace detect
| file    = File page text
| category = Category page text
| other    =
}}
}}
</pre>
</pre>


On any pages other than file and category pages the code above will render nothing.
== Parameters ==
<!-- Do not remove this one. It is supposed to render nothing, but we have it here for testing purposes. -->
: {{Namespace detect
  | file    = File page text
  | category = Category page text
  | other    =
  }}
 
By using an empty parameter, you can make it so the template doesn't render anything for some specific page type. Like this:
<pre>
{{Namespace detect
| main  =
| other = Other pages text
}}
</pre>
 
The code above will render nothing when on mainspace (article) pages, but will return this when on other pages:
: {{Namespace detect
  | main  =
  | other = Other pages text
  }}
 
== Demospace and page ==


For testing and demonstration purposes, this template can take two parameters named '''demospace''' and '''page'''.
* '''main''' - text to return if the page is in the main namespace.
* '''talk''' - text to return if the page is in a talk namespace. This can be any talk namespace - it will match any of "Talk:", "Wikipedia talk:", "User talk:", etc.
* Subject namespace parameters, e.g. '''wikipedia''', '''user''', '''file'''... - the text to return if the page is in the corresponding namespace. This module accepts all subject namespaces as parameters, including [[Wikipedia:Namespace#Aliases|namespace aliases]] and [[Wikipedia:Namespace#Virtual namespaces|virtual namespaces]]. See below for a list of supported values.
* '''other''' - text to return if no parameters for the page's namespace were specified. This text is also returned if {{para|demospace}} is set to an invalid namespace value.
* '''subjectns''' - if on a talk page, use the corresponding subject page. Can be set with values of "yes", "y", "true" or "1".
* '''demopage''' - specifies a page to detect the namespace of. If not specified, and if the {{para|demospace}} parameter is not set, then the module uses the current page.
* '''demospace''' - force the module to behave as if the page was in the specified namespace. Often used for demonstrations.  


'''demospace''' understands any of the page type names used by this template, including the '''other''' type. It tells the template to behave like it is on some specific type of page. Like this:
=== Namespace parameters ===
<pre>
{{Namespace detect
| main  = Article text
| other = Other pages text
| demospace = main
}}
</pre>


No matter on what kind of page the code above is used, it will return this:
Possible values for subject namespace parameters are as follows:
: {{Namespace detect
  | main  = Article text
  | other = Other pages text
  | demospace = main
  }}


The '''page''' parameter instead takes a normal pagename, making this template behave exactly as if on that page. The pagename doesn't have to be an existing page. Like this:
{{#invoke:Namespace detect|table}}
<pre>
{{Namespace detect
| user  = User page text
| other = Other pages text
| page  = User:Example
}}
</pre>


No matter on what kind of page the code above is used, it will return this:
== Table function ==
: {{Namespace detect
  | user  = User page text
  | other = Other pages text
  | page  = User:Example
  }}


It can be convenient to let your template understand the '''demospace''' and/or '''page''' parameter and send it on to the {{tl|Namespace detect}} template. Like this:
Use the following to display a table with the different possible namespace parameters:
<pre>
{{Namespace detect
| main  = Article text
| other = Other pages text
| demospace = {{{demospace|}}}
| page  = {{{page|}}}
}}
</pre>


If both the '''demospace''' and '''page''' parameters are empty or undefined, the template will detect page types as usual.
&#123;&#123;#invoke:Namespace detect|table|talk=''yes''&#125;&#125;


== Parameters ==
To include the parameter for talk namespaces, use {{para|talk|yes}}.


List of all parameters:
== Porting to different wikis ==
<pre>
{{Namespace detect
| main  =  
...
| other =
| demospace = {{{demospace|}}} / main / talk / user /
              wikipedia / file / mediawiki / template /
              help / category / portal / other
| page  = {{{page|}}} / User:Example
}}
</pre>


Note: Empty values to the "main" ... "other" parameters have special meaning.
This module is designed to be portable. To use it on a different wiki, all you need to do is to change the values in [[Module:Namespace detect/config]]. Instructions are available on that page.


== Technical details ==
== Technical details ==


Namespace "Image" was renamed to "File" on 11 December 2008. This template was updated to understand both names well before that, thus it still works fine. For backwards compatibility it still understands "image" both as a parameter name, such as "image&nbsp;= File page text", and as a value "demospace&nbsp;= image".
The module uses a data page at [[Module:Namespace detect/data]]. This page is loaded with [[mw:Extension:Scribunto/Lua reference manual#mw.loadData|mw.loadData]], which means it is processed once per page rather than once per #invoke. This was done for performance reasons.
 
If you intend to feed tables as content to the numbered parameters of this template, you need to know this:
 
[[Help:Template|Templates]] 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>. Thus templates can not handle [[Help:Table|wikitables]] as input unless you escape them by using the {{tn|!}} template. This makes it hard to use wikitables as parameters to templates. Instead, the usual solution is to use "[[Help:HTML in wikitext|HTML wikimarkup]]" for the table code, which is more robust.
 
For more technical details, e.g. about copying this template to other projects and CSS-based namespace detection, see {{tl|Main talk other}} and its talk page.
 
== See also ==
*[[Template:SUBJECTSPACE formatted]]
{{Namespace and pagename-detecting templates}}
 
<includeonly>{{Sandbox other|
| <!--Categories below this line, please; interwikis at Wikidata-->
[[Category:Namespace manipulation templates]]
[[Category:If-then-else templates]]
}}</includeonly>

Revision as of 09:55, 25 May 2025

This module allows you to output different text depending on the namespace that a given page is in. It is a Lua implementation of the {{namespace detect}} template, with a few improvements: all namespaces and all namespace aliases are supported, and namespace names are detected automatically for the local wiki.

Usage

{{#invoke: Namespace detect | main
| page              = <!-- page to detect namespace for, if not the current page -->
| main              = <!-- text to return for the main namespace -->
| talk              = <!-- text to return for talk namespaces -->

<!-- text to return for specific subject namespaces -->
| portal            = 
| category          = 
| user 	            = 
| draft             =
| wikipedia         = 
| mediawiki         = 
| book              = 
| timedtext         = 
| template          = 
| special           = 
| media             = 
| file              = 
| help 	            = 
| module            = 

| other             = <!-- text to return for unspecified namespaces -->
| demospace         = <!-- namespace to display text for -->

| subjectns         = <!-- set to "yes" to treat talk pages as the corresponding subject page -->
}}

Parameters

  • main - text to return if the page is in the main namespace.
  • talk - text to return if the page is in a talk namespace. This can be any talk namespace - it will match any of "Talk:", "Wikipedia talk:", "User talk:", etc.
  • Subject namespace parameters, e.g. wikipedia, user, file... - the text to return if the page is in the corresponding namespace. This module accepts all subject namespaces as parameters, including namespace aliases and virtual namespaces. See below for a list of supported values.
  • other - text to return if no parameters for the page's namespace were specified. This text is also returned if |demospace= is set to an invalid namespace value.
  • subjectns - if on a talk page, use the corresponding subject page. Can be set with values of "yes", "y", "true" or "1".
  • demopage - specifies a page to detect the namespace of. If not specified, and if the |demospace= parameter is not set, then the module uses the current page.
  • demospace - force the module to behave as if the page was in the specified namespace. Often used for demonstrations.

Namespace parameters

Possible values for subject namespace parameters are as follows:

Namespace Aliases
main
user
fallenchungus wiki project
file image
mediawiki
template
help
category
timedtext
module

Table function

Use the following to display a table with the different possible namespace parameters:

{{#invoke:Namespace detect|table|talk=yes}}

To include the parameter for talk namespaces, use |talk=yes.

Porting to different wikis

This module is designed to be portable. To use it on a different wiki, all you need to do is to change the values in Module:Namespace detect/config. Instructions are available on that page.

Technical details

The module uses a data page at Module:Namespace detect/data. This page is loaded with mw.loadData, which means it is processed once per page rather than once per #invoke. This was done for performance reasons.