Template:Anchor/doc: Difference between revisions

>PJosepherum
m 1 revision
iw>FDMS4
m harmonisation
Line 1: Line 1:
{{Documentation subpage}}
{{documentation subpage}}
{{high-use|20,000+}}
{{lua|Module:Anchor}}
<!-- PLEASE ADD CATEGORIES AND INTERWIKIS LINKS AT THE END OF THIS PAGE -->


The template <nowiki>{{</nowiki>[[{{ns:Template}}:{{BASEPAGENAME}}|{{lc:{{BASEPAGENAME}}}}]]<nowiki>}}</nowiki> inserts one or more [[Fragment identifier|HTML fragment identifiers]] (anchor names) in a page. Those locations can then be linked to using <code><nowiki>[[#Location|]]</nowiki></code> syntax. {{#ifeq:{{BASEPAGENAME}}|Visible anchor|&nbsp;Unlike {{tl|Anchor}}, the first parameter will be visible text on the page.|}} (Usually the first letter of the ''location'' is capitalised to reflect the common capitalisation used in section headers – see [[MOS:HEAD]].)
==Usage==
1. <code style="color:green;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}|foo<nowiki>}}</nowiki></code>
: could be linked to with <tt><nowiki>[[#foo|...]]</nowiki></tt> from within the same article,
: or it could be linked to with <tt><nowiki>[[</nowiki>''<small>articlename</small>''#foo|...<nowiki>]]</nowiki></tt> from other articles and from redirects.


=== Examples ===
2. Anchors can be more suitable for inter-article linking than section titles are. For example,
:<code style="color:green;"><nowiki>=={{</nowiki>{{lc:{{BASEPAGENAME}}}}|foo<nowiki>}}</nowiki></code><code style="color:gray;"> Section title <nowiki>==</nowiki></code>
:Here, links via <tt><nowiki>[[</nowiki>''articlename''#foo<nowiki>]]</nowiki></tt> would remain valid even if the section were renamed.


1. <code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}|Foo<nowiki>}}</nowiki></code>  
3. The template can be used to create up to ten anchors with a single call. For example,
: could be linked to with <tt><nowiki>[[#Foo|…]]</nowiki></tt> from within the same article,  
: <code style="color:green;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}|foo|bar|baz<nowiki>}}</nowiki></code>  
: 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.
: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>.
:Specifying more than 10 anchors will cause an error message to be displayed.


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" /></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,
==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 ===
==== <nowiki>{{</nowiki>anchor|Example anchor}} Usage ====
or
==== <nowiki>{{</nowiki>anchor|anchor=Example anchor}} Usage ====


{|class="wikitable" style="float:right"
''[[Template:Anchor#Example anchor]] (from another page) or [[#Example anchor]] (from this page) will now link to the section called "Usage", as seen above.''
!Character
![[Character entity reference|Code]]
!Template
!Meaning
|-
|&quot;
|<code>&amp;quot;</code> <br /><code>&amp;#34;</code>
|{{N/a}}
|(double)&nbsp;[[quotation mark]]
|-
|&#35;
|<code>&amp;#35;</code>
|{{N/a}}
|[[Number sign|hash]]
|-
|&#124;
|<code>&amp;#124;</code>
|{{tl|!}}
|[[Vertical bar|pipe]]
|-
|&#61;
|<code>&amp;#61;</code>
|{{tl|{{=}}}}
|[[Equals sign|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 "&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]].
* 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 {{nowrap|1="<code><nowiki>/* {{anchor|Issues}}Limitations */ New issue</nowiki></code>"}}. Also, when the section is saved, browsers may not return to the section. Consider using <code><nowiki><span id="..." /></nowiki></code> 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===
<includeonly>
Anchors may be used within tables, subject to certain restrictions. The {{tlx|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:<source enclose=div lang=text>|+ {{anchor|FooX}} A table caption</source> and the following forms of cell are valid:<source enclose=div lang=text>!{{anchor|Foo1}} A header cell
[[Category:Formatting templates]]
!style="background:white;" |{{anchor|Foo2}} A header cell with styling
[[Category:Internal link templates]]
|{{anchor|Foo3}} A data cell
[[ar:template:anchor]]
|rowspan=2 |{{anchor|Foo4}} A data cell spanning two rows</source> You need to ensure that the {{tlx|anchor}} is not in that portion of the markup intended for the classes, styles etc. Thus, {{tlx|anchor}} cannot be placed anywhere on lines that begin with <code><nowiki>{|</nowiki></code> (start of table) or <code><nowiki>|-</nowiki></code> (new row), and the following forms of cell are not valid:<source enclose=div lang=text>!{{anchor|Foo1}} |A header cell
[[bg:template:anchor]]
!style="background:white;" {{anchor|Foo2}} |A header cell with styling
[[ca:template:anchor]]
|{{anchor|Foo3}} |A data cell
[[cs:template:anchor]]
|rowspan=2 {{anchor|Foo4}} |A data cell spanning two rows</source> If it is necessary for an anchor to be in any of these positions, a different technique is used - the <code>id=</code> attribute. This is placed in that portion of the markup where the classes, styles etc. may be used, as follows:<source enclose=div lang=text>{| id=FooX class=wikitable
[[da:template:anchor]]
|- id=FooY
[[de:Vorlage:Anker]]
!id=Foo1 |A header cell
[[el:template:anchor]]
!style="background:white;" id=Foo2 |A header cell with styling
[[en:template:anchor]]
|id=Foo3 |A data cell
[[eo:template:anchor]]
|rowspan=2 id=Foo4 |A data cell spanning two rows</source> The <code>id=</code> attribute may appear before, between or after any other attributes that may be present, but only one <code>id=</code> attribute may be used in each of these areas.
[[eu:template:anchor]]
 
[[fa:template:anchor]]
=== TemplateData ===
[[fi:template:anchor]]
{{TemplateDataHeader}}
[[fr:template:anchor]]
<templatedata>{
[[hsb:template:anchor]]
"description": "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.",
[[he:תבנית:עוגן2]]
"params": {
[[hu:template:anchor]]
  "1": {
[[is:template:anchor]]
    "label": "First anchor",
[[ja:template:anchor]]
    "description": "First anchor; Only the first anchor is required.",
[[ko:template:anchor]]
    "type": "string",
[[ks:template:anchor]]
    "required": true
[[la:template:anchor]]
  },
[[mi:template:anchor]]
  "2": {
[[nds:template:anchor]]
    "label": "Second anchor",
[[nl:template:anchor]]
    "description": "Second anchor.",
[[nn:template:anchor]]
    "type": "string",
[[no:template:anchor]]
    "required": false
[[oc:template:anchor]]
  },
[[pl:template:anchor]]
  "3": {
[[ps:template:anchor]]
    "label": "Third anchor",
[[pt:template:anchor]]
    "description": "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.",
[[rmy:template:anchor]]
    "type": "string",
[[ro:template:anchor]]
    "required": false
[[ru:template:anchor]]
  }
[[sd:template:anchor]]
}}</templatedata>
[[simple:template:anchor]]
 
[[sl:template:Sidro]]
=== See also ===
[[sq:template:anchor]]
* {{#ifeq:{{BASEPAGENAME}}|Visible anchor|{{tl|Anchor}}|{{tl|Visible anchor}}}}
[[sr:template:anchor]]
* {{tl|Anchored list}}
[[sv:template:anchor]]
* {{tl|Anchor comment}}
[[sw:template:anchor]]
* {{tl|Shortcut}}
[[te:template:anchor]]
* [[WP:ANCHOR]]
[[tn:template:anchor]]
* [[WP:TARGET]]
[[uk:template:anchor]]
 
[[ur:template:anchor]]
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox||
[[vi:template:anchor]]
 
[[yi:template:anchor]]
<!-- CATEGORIES AND INTERWIKIS LINKS HERE, THANKS -->
[[zh:template:anchor]]
[[Category:Wikipedia formatting and function templates]]
</includeonly>
 
}}</includeonly>