Jump to content

Template:Anchor/doc: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Update
→‎Examples: fix quotes
(2 intermediate revisions by the same user not shown)
Line 8: Line 8:
=== Examples ===
=== Examples ===


1. <code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}|Foo<nowiki>}}</nowiki></code>
# <code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}|Foo<nowiki>}}</nowiki></code> <br> could be linked to with <tt><nowiki>[[#Foo|…]]</nowiki></tt> from within the same article, <br> or it could be linked to with {{nowrap|<tt><nowiki>[[</nowiki>''<small>Article name</small>''#Foo<nowiki>|…]]</nowiki></tt>}} from other articles and from redirects.
# Anchors can be more suitable for inter-article linking than section titles are. For example: <br> {{nowrap|1=<code style="color:dimgray;"><nowiki>==</nowiki><span>{{sp}}</span><code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}<nowiki>|Foo}}</nowiki></code><span>{{sp}}</span>Section title <nowiki>==</nowiki></code>}} <br> Here, links via {{nowrap|1=<tt><nowiki>[[</nowiki>''Article name''#Foo<nowiki>]]</nowiki></tt>}} would remain valid even if the section were renamed. (Note that the anchor is placed ''before'' the section name; otherwise browsers may hide the section title from view.) However, as noted under [[#Limitations|Limitations]] below, it may be preferable to use direct HTML rather than the template within section titles: <br> {{nowrap|1=<code style="color:dimgray;"><nowiki>==</nowiki><span>{{sp}}</span><code style="color:darkgreen;"><nowiki><span id="Foo"></span></nowiki></code><span>{{sp}}</span>Section title <nowiki>==</nowiki></code>}}
: could be linked to with <tt><nowiki>[[#Foo|…]]</nowiki></tt> from within the same article,
# The template can be used to create multiple anchors with a single call. For example, <br> {{nowrap|1=<code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}<nowiki>|Foo|Bar|baz}}</nowiki></code>}} <br> will create three anchors that can then be linked to with <tt><nowiki>[[#Foo]]</nowiki></tt>, <tt><nowiki>[[#Bar]]</nowiki></tt> and <tt><nowiki>[[#baz]]</nowiki></tt>.
: or it could be linked to with {{nowrap|<tt><nowiki>[[</nowiki>''<small>Article name</small>''#Foo<nowiki>|…]]</nowiki></tt>}} from other articles and from redirects.

2. Anchors can be more suitable for inter-article linking than section titles are. For example:
:{{nowrap|1=<code style="color:dimgray;"><nowiki>==</nowiki><span>{{sp}}</span><code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}<nowiki>|Foo}}</nowiki></code><span>{{sp}}</span>Section title <nowiki>==</nowiki></code>}}
:Here, links via {{nowrap|1=<tt><nowiki>[[</nowiki>''Article name''#Foo<nowiki>]]</nowiki></tt>}} would remain valid even if the section were renamed. (Note that the anchor is placed ''before'' the section name; otherwise browsers may hide the section title from view.) However, as noted under [[#Limitations|Limitations]] below, it may be preferable to use direct HTML rather than the template within section titles:
:{{nowrap|1=<code style="color:dimgray;"><nowiki>==</nowiki><span>{{sp}}</span><code style="color:darkgreen;"><nowiki><span id="Foo"></span></nowiki></code><span>{{sp}}</span>Section title <nowiki>==</nowiki></code>}}

3. The template can be used to create multiple anchors with a single call. For example,
: {{nowrap|1=<code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}<nowiki>|Foo|Bar|baz}}</nowiki></code>}}
:will create three anchors that can then be linked to with <tt><nowiki>[[#Foo]]</nowiki></tt>, <tt><nowiki>[[#Bar]]</nowiki></tt> and <tt><nowiki>[[#baz]]</nowiki></tt>.


=== Limitations ===
=== Limitations ===
Line 49: Line 40:
|[[Equals sign|equals]]
|[[Equals sign|equals]]
|}
|}
* This template should not be used within section headings. Doing so will result in broken links in edit summaries.
* 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 "&amp;#" codes shown for them here. Or, the pipe symbol and equals sign can be worked around with {{tl|!}} and {{tl|{{=}}}}, respectively. Markup code such as {{tag|sup|o}} and {{tag|sub|o}} (superscript and subscript) cannot be used. Most other characters, including white space and punctuation, are not a problem.
* 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 "&amp;#" codes shown for them here. Or, the pipe symbol and equals sign can be worked around with {{tl|!}} and {{tl|{{=}}}}, respectively. Markup code such as {{tag|sup|o}} and {{tag|sub|o}} (superscript and subscript) cannot be used. Most other characters, including white space and punctuation, are not a problem.
* Anchor names should be unique on a page, and should not duplicate any heading titles. Duplicate anchors will not work as expected since the <code><nowiki>#location</nowiki></code> 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 names should be unique on a page, and should not duplicate any heading titles. Duplicate anchors will not work as expected since the <code><nowiki>#location</nowiki></code> 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]].

Revision as of 03:38, 5 May 2015

The template {{anchor}} inserts one or more HTML fragment identifiers (anchor names) in a page. Those locations can then be linked to using [[#Location|…]] syntax. (Usually the first letter of the location is capitalised to reflect the common capitalisation used in section headers – see MOS:HEAD.)

Examples

  1. {{anchor|Foo}}
    could be linked to with [[#Foo|…]] from within the same article,
    or it could be linked to with [[Article name#Foo|…]] from other articles and from redirects.
  2. Anchors can be more suitable for inter-article linking than section titles are. For example:
    == {{anchor|Foo}} Section title ==
    Here, links via [[Article name#Foo]] would remain valid even if the section were renamed. (Note that the anchor is placed before the section name; otherwise browsers may hide the section title from view.) However, as noted under Limitations below, it may be preferable to use direct HTML rather than the template within section titles:
    == <span id="Foo"></span> Section title ==
  3. The template can be used to create multiple anchors with a single call. For example,
    {{anchor|Foo|Bar|baz}}
    will create three anchors that can then be linked to with [[#Foo]], [[#Bar]] and [[#baz]].

Limitations

Character Code Template Meaning
" &quot;
&#34;
(double) quotation mark
# &#35; hash
| &#124; {{!}} pipe
= &#61; {{=}} equals
  • This template should not be used within section headings. Doing so will result in broken links in edit summaries.
  • 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 should be unique on a page, and should 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.
  • If the template is added to a section title then the code will appear in the edit summary window when that section is edited, as in "/* {{anchor|Issues}}Limitations */ New issue". Also, when the section is saved, browsers may not return to the section. Consider using <span id="..."></span> directly, rather than using the anchor template, when in a section title.
  • Anchor links are case sensitive in some browsers, so treat all anchor links as case sensitive.

Use in tables

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. It is used on the table's caption thus:

|+ {{anchor|FooX}} A table caption

and the following forms of cell are valid:

!{{anchor|Foo1}} A header cell
!style="background:white;" |{{anchor|Foo2}} A header cell with styling
|{{anchor|Foo3}} A data cell
|rowspan=2 |{{anchor|Foo4}} A data cell spanning two rows

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:

!{{anchor|Foo1}} |A header cell
!style="background:white;" {{anchor|Foo2}} |A header cell with styling
|{{anchor|Foo3}} |A data cell
|rowspan=2 {{anchor|Foo4}} |A data cell spanning two rows

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:

{| id=FooX class=wikitable
|- id=FooY
!id=Foo1 |A header cell
!style="background:white;" id=Foo2 |A header cell with styling
|id=Foo3 |A data cell
|rowspan=2 id=Foo4 |A data cell spanning two rows

The id= attribute may appear before, between or after any other attributes that may be present, but only one id= attribute may be used in each of these areas.

TemplateData

This is the TemplateData for this template used by TemplateWizard, VisualEditor and other tools. See a monthly parameter usage report for Template:Anchor in articles based on its TemplateData.

TemplateData for Anchor

The template {{anchor}} inserts one or more HTML anchors in a page. Those locations can then be linked to using [[#location|…]] syntax. The parameters here are for convenience; no parameter name is required in the template itself.

Template parameters

ParameterDescriptionTypeStatus
First anchor1

First anchor; Only the first anchor is required.

Stringrequired
Second anchor2

Second anchor.

Stringoptional
Third anchor3

Third anchor. For additional anchors, just type in 4 as the parameter name for the next, 5 for the next after that, and so on.

Stringoptional

See also