• Home
  • Random
  • Nearby
  • Log in
  • Settings
Donate Now If Wikipedia is useful to you, please give today.
  • About Wikipedia
  • Disclaimers
Wikipedia

User:Davidgothberg/When on page

  • User page
  • Talk
  • Language
  • Watch
  • Edit
< User:Davidgothberg


Documentation[view] [edit] [history] [purge]

This is the {{When on page}} meta-template.

It helps other templates detect what page they are on. It uses pattern matching on the pagename. It can reuse the same input on multiple matching patterns.

This template is an extended version of {{When pagename is}}.

Contents

  • 1 Usage
  • 2 Partial subpage names
  • 3 "page" parameter
  • 4 Namespace matching
  • 5 Technical details
  • 6 See also

Usage

edit

This template takes two or more parameters. The data to return when a match is found is fed as numbered parameters. Most of the other parameters don't have fixed names, instead they are part of the pattern matching. Like this:

{{When on page
 | 1 = Doc and sandbox text
 | 2 = Other pages text
 | /doc = 1
 | /sandbox = 1
 | other = 2
}}

If the template is on "Template:Example/doc" the code above will return this:

Doc and sandbox text

If the template is on any other page than a /doc or /sandbox page it will return this:

Other pages text

Here is a description of the full pattern matching:

{{When on page
<!-- Match on full pagename -->
 | 1 = Text for "User:Example/test" and "User:Example".
 | 2 = Text for "User talk:Example".
 | User:Example/test = 1
 | User:Example = 1
 | User talk:Example = 2

<!-- Match on full basepage name, when on a basepage or its subpages -->
 | 3 = Matches "User:Example" and "User:Example/test".
 | User:Example/+ = 3

<!-- Match on pagename, when on a basepage -->
 | 4 = Matches "User:Example", "User talk:Example", "Template:Example" 
       and so on, but not "User:Example/something".
 | Example = 4

<!-- Match on full basepage name, when on a subpage -->
 | 5 = Matches "User:Example/something", but not "User:Example".
 | 6 = Matches "User talk:Example/something".
 | User:Example/* = 5
 | User talk:Example/* = 6

<!-- Match on basepage name, when on a subpage -->
 | 7 = Matches "User:Example/something" and "User talk:Example/something".
 | Example/* = 7

<!-- Match on subpage name -->
 | 8 = Any pagename that ends in "/something" or "/doc".
 | /something = 8
 | /doc = 8

<!-- Match on partial subpage name (case-insensitive) -->
 | 9 = Any subpage name beginning with "/some" or "/Some".
 | 10 = Matches "User talk:Example/Archive 1".
 | /some* = 9
 | /arch* = 10

<!-- Defaults -->
 | 11 = Text for any basepage.
 | 12 = Text for any subpage.
 | 13 = Text for any page.
 | basepage = 11
 | subpage = 12
 | other = 13
}}

The matching goes from top to bottom, and returns the first parameter that matches. "Top to bottom" means the order shown above, not the order you happen to feed the parameters. The numbered parameters don't need to be contiguous, you could just as well use 3, 5, 120 and so on.

There's no limit to the number of parameters (patterns or numbered) that you can use, other than what the servers and MediaWiki system can handle.

Most of the matching is case-sensitive. For instance "/test" matches "User:Example/test" but not "User:Example/Test".

Matching on partial subpage names such as "/some*" has some limitations, see its own section below.

If an empty (but defined) parameter matches, the matching stops and the template returns an empty string. That's on purpose and can be used like this:

{{When on page
 | 1 = Sandbox text
 | 2 = Other pages text
 | /doc =
 | /sandbox = 1
 | other = 2
}}

The code above will render nothing when on a /doc page. But when on a /sandbox page it will return this:

Sandbox text

And when on any other page it will return this:

Other pages text

Partial subpage names

edit

This template can also match on partial subpage names. Like this:

{{When on page
 | 1 = Archive page text
 | 2 = Other pages text
 | /archiv* = 1
 | other = 2
}}

If on "User:Example/Archive 5" the code above will return this:

Archive page text

The parameter name "/some*" must be lower case. But it matches subpage names in both upper and lower case such as "User:Example/SomeThing" and "User:Example/something".

The partial matching only supports matching on 4, 6 and 8 characters. Thus using "/some*", "/someth*" and "/somethin*" works, but using "/som*" or "/somet*" doesn't work.

Longer patterns match first, thus if both "/somethin*" and "/some*" are defined, and the current page is "User:Example/Something", then the data from "/somethin*" will be used.

"page" parameter

edit

For testing and demonstration purposes this template can take a parameter named page. Like this:

{{When on page
 | 1 = Test pages text
 | 2 = Other pages text
 | /test = 1
 | other = 2
 | page = Template:Example/test
}}

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

Test pages text

The page parameter makes this template behave exactly as if on that page. The pagename doesn't have to be an existing page.

If the page 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:

{{When on page
 | 1 = Test pages text
 | 2 = Other pages text
 | /test = 1
 | other = 2
 | page = {{{page|}}}
}}

Namespace matching

edit

This template doesn't have namespace matching. If you need that then combine this template with one of the namespace-detection templates such as {{When on template page}}. Like this:

{{When on template page
 | {{When on page
    | 1 = Template doc page text.
    | /doc = 1
   }}
 | <!--(action if not on a Template: page)-->
}}

If on "User:Example/doc" the code above will return nothing. But if on "Template:Example/doc" it will return this:

Template doc page text.

Technical details

edit

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.

For more technical details, see the talk page.

See also

edit
  • v
  • t
  • e
Namespace and pagename-detecting templates
Namespace
Particular
  • Main other
  • Talk other
  • Category other
  • Draft other
  • File other
    • Is Commons
  • Help other
  • Module other
  • Portal other
  • Template other
  • User other
  • Project other
  • Single namespace
  • If talkspace of
More generic
  • Namespace detect
  • Namespace detect showall
  • Main talk other
    • Main talk other flex
  • Talkspace detect
Pagename
When on basepage
Detects and acts according to whether it is on a basepage or on any subpage (that is, a ../.. subpage).
When pagename is
For pattern matching on the pagename.
Module:Page
Can take apart and put together pagenames in several ways.
IP-talk
Returns IP-user talkpage, registered-user talkpage or non-user talkpage.
IP-user other
For detecting IP-user pages.
Subpage other
Pagename has an after-slash part (not in mainspace)
Sandbox other
Page is subpage /sandbox (template space)
Testcases other
Page is subpage /testcases (template space)
Rootpage other
Page is rootpage (vs. /subpage) (template space)
Others
Stub other
Detects whether it is on a page with a stub template or not.
Redirect other
Detects whether it is on a redirect or not.
If preview
Detects whether page is viewed as a preview or not.
If IP
Displays different content to IP users and logged-in users.
If autoconfirmed
Displays different content to autoconfirmed and non-autoconfirmed users.
If extended confirmed
Displays different content to extended confirmed and non-extended confirmed users.
If administrator
Displays content only to admins.
If mobile
Displays different content to mobile users and desktop users.
If dark
Displays different content to dark mode users and light mode users.
Noscript
Detects whether page is viewed with a JavaScript-capable client or not.
  • Wikipedia:Namespace
  • Wikipedia:Page name
  • Magic words
  • ParserFunctions
The above documentation is transcluded from User:Davidgothberg/When on page/doc. (edit | history)
Editors can experiment in this template's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this page.
Retrieved from "https://en.wikipedia.org/w/index.php?title=User:Davidgothberg/When_on_page&oldid=934826865"
Last edited on 8 January 2020, at 18:46

Languages

      This page is not available in other languages.

      Wikipedia
      • Wikimedia Foundation
      • Powered by MediaWiki
      • This page was last edited on 8 January 2020, at 18:46 (UTC).
      • Content is available under CC BY-SA 4.0 unless otherwise noted.
      • Privacy policy
      • About Wikipedia
      • Disclaimers
      • Contact Wikipedia
      • Code of Conduct
      • Developers
      • Statistics
      • Cookie statement
      • Terms of Use
      • Desktop