Template:Anchor: Difference between revisions
en>Plastikspork (Fix |=FOO bug where {{anchor|=FOO}} transcludes Template:FOO) |
m (1 revision imported) |
(One intermediate revision by the same user not shown) | |
(No difference)
|
Latest revision as of 20:10, 19 June 2023
File:Lua-Logo.svg | This template uses Lua: |
This template is used in system messages, and on approximately 76,000 pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, any changes should be tested in the template's /sandbox or /testcases subpages, or in your own user subpage. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
File:Kawaii robot power clipart.svg | This template is used by one or more bots.
If you intend to change this template in any significant way, move or nominate it for deletion, please inform the bot operators. Thank you. The relevant bots are: User:Cewbot (BRFA). |
The template {{anchor}} inserts one or more invisible anchor names (HTML fragment identifiers) in a page.
The basic format is {{anchor|Anchor name}}
. To link to an anchor from within the same page, use [[#Anchor name|display text]]
. To link to an anchor from another page, use [[Article name#Anchor name|display text]]
. See Help:Section linking for more details.
Note that #Anchor name
, used by the MediaWiki software to (usually) direct users to sections within a page, is not a wikitext directive like #redirect
.
Anchors are most useful with sections, since links to a section will not work if a section is renamed. However, anchors can be used as needed anywhere in an article. The anchor name can be arbitrary, but the first letter should generally be capitalized in keeping with header style (see Manual of Style: Section headers for more information).
Basic format
You can define one or more anchor names, separated by |. This can be useful for alternative spellings, such as "humor" and "humour."
In general, if the intended target of an anchor is a section title, then it should be placed at the end of the section header by substitution:
== Section header{{subst:anchor|Anchor name}} ==
which will be saved in the article as:
== Section header
This template is used on 76,000+ pages and changes may be widely noticed. Test changes in the template's /sandbox or /testcases subpages, or in your own user subpage. Consider discussing changes on the talk page before implementing them.
Usage
Wraps a short span of text in <syntaxhighlight>
tags (see mw:Extension:SyntaxHighlight). This template should be used for short samples; longer content should use <pre>...</pre>
or <syntaxhighlight>...</syntaxhighlight>
. See Help:Wiki markup for an explanation of what the various tags do.
If the content includes an equals sign (=), you must specify the parameter explicitly: {{code|1=date=30 Feb 2010}}
.
The template uses the <syntaxhighlight>
tag with the attribute inline=1
. This works like the combination of the <code>
and <nowiki>
tags, applied to the expanded wikitext. For example, {{code|some '''wiki''' text}}
will not render the word "wiki" in bold, and will render the tripled-single-quotes:
- Template loop detected: Template:Code
However, {{code|a {{template}} call}}
will still invoke the template:
- Template loop detected: Template:Code
Use <nowiki>...</nowiki>
around the template name to avoid this problem:
- Template loop detected: Template:Code
When used inline with regular text, {{code}}
generally looks best and is easiest to read when it is explicitly spaced apart from the regular text:
- Template loop detected: Template:Code
is well spaced:
- foo Template loop detected: Template:Code quux.
versus:
- Template loop detected: Template:Code
which is going to be visually confusing for many:
- foo Template loop detected: Template:Code quux.
because "foo" and "Template loop detected: Template:Code" will seem more closely associated than "Template loop detected: Template:Code" and "Template loop detected: Template:Code"; the width of the space character in a monospaced font is almost always larger than in a proportional font.
Use parameter {{{2}}} (unnamed, as |2=
, or more explicitly as |lang=
) to specify a language for mw:Extension:SyntaxHighlight. This option defaults to plain-text, i.e. no highlighting. There is no highlighting option for wikitext as a markup language, though Template loop detected: Template:Code and Template loop detected: Template:Code are valid values, as are Template loop detected: Template:Code, Template loop detected: Template:Code, Template loop detected: Template:Code, Template loop detected: Template:Code, Template loop detected: Template:Code and many others. Attempting to use an invalid one causes a list of valid ones to be displayed in place of the template output, when the page is previewed or saved.
This template does not need to be substituted.
Examples
Markup
Renders asScript error: No such module "For loop".
Included templates
Embedded templates do not function as expected inside {{code}}; for longer, free-form blocks of code, which can contain templates such as {{var}} and {{samp}}, use <code>...</code>
as a wrapper instead of this template.
Templates used inside {{code}} expose the rendered HTML— this can be useful. For example:
Markup
Renders asScript error: No such module "For loop".
The above example shows the HTML rendered by the citation template, including the hidden metadata.
TemplateData
This is the TemplateData for this template used by TemplateWizard, VisualEditor and other tools. Click here to see a monthly parameter usage report for this template based on this TemplateData.
TemplateData for Anchor
Script error: No such module "Format TemplateData".
See also
- {{codett}}, wrapper for this template that removes background and border styling
- {{mono}}, monospaced font only
- {{param value}}, monospaced; friendly handling for blank space
- {{Syntaxhighlight}}
- {{kbd}}, for keyboard input
- Help:Wiki markup
==
Anchor names are case sensitive, so [[#Anchor name]]
and [[#Anchor Name]]
are not equivalent. When making an anchor, in general capitalize the first letter of the anchor name, and follow the same capitalization guidelines as headers (see Manual of Style: Headers for more information).
Limitations
Character | Code | Template | Meaning |
---|---|---|---|
" | " "
|
— | (double) quotation mark |
# | #
|
{{hash}} | hash |
| | |
|
{{!}} | pipe |
= | =
|
{{=}} | equals |
- Anchor names that contain any character shown in the table on the right will not work as expected. However, any of these characters can be replaced with the "&#" codes shown for them here. Or, the pipe symbol and equals sign can be worked around with {{!}} and {{=}}, respectively. Markup code such as
<sup>
and<sub>
(superscript and subscript) cannot be used. Most other characters, including white space and punctuation, are not a problem. - Anchor names must be unique on a page, and must not duplicate any heading titles. Duplicate anchors will not work as expected since the
#location
links go to the first anchor with that name. Duplicate anchors result in invalid HTML; you can check for duplicate anchors by running the page through the W3C Markup Validation Service. - Anchor links are case-sensitive in some browsers, so treat all anchor links as case-sensitive when creating links to them. For example, if you create the anchor with
=== Baz{{subst:anchor|Bar}} ===
, link to it with[[pagename#Bar]]
, not[[pagename#bAR]]
. However, because some browsers are not case-sensitive, do not create section titles or anchors that differ only in case from others on the page. That is, do not create both=== Abcd{{subst:anchor|Baz}} ===
and=== Efgh{{subst:anchor|bAZ}} ===
. - If a template is left unsubstituted inside a section title (which should never be done), then the template code will appear in the edit summary window each time a section edit of that section begins, as in
/* Limitations{{anchor|Issues}} */ New issue
. The editor must manually remove the anchor template from the edit summary window, or else when they save the section, the browser may not return to the section and the section link of that edit in the history page won't work.
Use in tables
To see how to set anchors in tables, see Help:Table#Section link or map link to a row anchor. Only use this template in tables if you need multiple anchors for the same row. |
Anchors may be used within tables, subject to certain restrictions. The {{anchor}}
template may be used in the caption and cells of a table, but not those portions of a table that are outside the caption and cells. Unlike in section headings, the template does not have to be substituted. It is used on the table's caption thus:
<syntaxhighlight lang=text>
|+ A table caption
</syntaxhighlight>
and the following forms of cell are valid:
<syntaxhighlight lang=text>
! A header cell
!style="background:white;" | A header cell with styling
| A data cell
|rowspan=2 | A data cell spanning two rows
</syntaxhighlight>
You need to ensure that the {{anchor}}
is not in that portion of the markup intended for the classes, styles etc. Thus, {{anchor}}
cannot be placed anywhere on lines that begin with {|
(start of table) or |-
(new row), and the following forms of cell are not valid:
<syntaxhighlight lang=text>
! |A header cell
!style="background:white;" |A header cell with styling
| |A data cell
|rowspan=2 |A data cell spanning two rows
</syntaxhighlight>
If it is necessary for an anchor to be in any of these positions, a different technique is used—the id=
attribute. This is placed in that portion of the markup where the classes, styles etc. may be used, as follows:
<syntaxhighlight lang=text>
A header cell | A header cell with styling | A data cell | A data cell spanning two rows
</syntaxhighlight>
The If Rationale for substitution in the headerTo show why substitution in the header is preferred, the following anchor is placed in the Basic format section header:
Since the anchor is actually used on this documentation page, the links #Basic format, #Foo and #In-Foo all work and can be tested to see the effect. For contrast, an anchor named #Under-Foo has also been placed underneath the Basic format section header and can be clicked to illustrate how this placement hides the section title when jumping to the linked target:
Another anchor named #Above-Foo has been placed above the section header. This anchor does work correctly, but because the anchor is technically not in the section but before it, it makes editing counter-intuitive.
Anchors should also never be left unsubstituted within a section header, as this violates MOS:HEADINGS and causes technical complications. Explanations and examplesReasons for the above being best practice are detailed in the following additional, numbered examples, as well as in § Limitations section:
This provides the stable, linkable anchor, but without the edit problem. The Note above still applies. See § Limitations for details.
TemplateDataThis is the TemplateData for this template used by TemplateWizard, VisualEditor and other tools. Click here to see a monthly parameter usage report for this template based on this TemplateData.
TemplateData for Anchor Add HTML anchors to a page. Make a link to your anchor with [[#Anchor on same page]] or [[Page you're linking to#Anchor on that page]]. Every anchor on a page must be different. Do not use the characters " or # or | or =.
See also
|
---|
- Pages with script errors
- Pages with template loops
- Pages with TemplateStyles errors
- Pages with broken file links
- Lua-based templates
- Pages used in system messages needing protection
- Pages monitored by Wikipedia bots
- Templates using TemplateData
- Programming typing-aid templates
- Semantic markup templates
- Wikipedia XHTML tag-replacing templates
- Wikipedia formatting and function templates
- Wikipedia section templates
- Templates with no visible output