Difference between revisions of "Template:Anchor/doc"

From Summertime Saga Wiki
Jump to: navigation, search
(Created page with "The template {{tl|anchor}} inserts one or more invisible HTML fragment identifiers (anchor names) in a page. Those locations can then be linked to using <code><nowiki>#Locat...")
 
m
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
The template {{tl|anchor}} inserts one or more invisible HTML fragment identifiers (anchor names) in a page. Those locations can then be linked to using <code><nowiki>[[#Location|...]]</nowiki></code> syntax (note that <code><nowiki>#Location</nowiki></code> here is not a browser instruction like <code><nowiki>#redirect</nowiki></code>—the word "Location" can be any word you wish to associate with any part of an article in which you decide to place the {{tlx|anchor}} template, and can be entirely arbitrary; also, the first letter of the ''location'' is usually capitalised to reflect the common capitalisation used in section headers.
+
The template {{tlg|anchor}} inserts one or more invisible HTML fragment identifiers (anchor names) in a page. Those locations can then be linked to using <code><nowiki>[[#Location|...]]</nowiki></code> syntax. Note that <code><nowiki>#Location</nowiki></code> here is not a browser instruction like <code><nowiki>#REDIRECT</nowiki></code>—the word “Location” can be any word you wish to associate with any part of an article in which you decide to place the {{tlg|code=yes|nolink=yes|anchor}} template, and can be entirely arbitrary; also, the first letter of the location is usually capitalised to reflect the common capitalisation used in section headers.
  
 
{{anchor|Foo|Foo bar}}
 
{{anchor|Foo|Foo bar}}
== Examples ==
+
== Usages ==
  
Common best practice (for section headers, the most common use case):
+
<code><nowiki>{{</nowiki>anchor|Foo|Foo bar}}
  
<code style="color:darkgreen;"><nowiki> {{</nowiki>anchor|Foo|Foo bar}}
+
== Examples ==</code>
 +
 
 +
[[#Examples]], [[#Foo]] and [[#Foo bar]] all work.
 +
 
 +
== Best practices ==
 +
 
 +
Reasons for the above being best practice are detailed in the following additional, numbered examples, as well as in {{section link||Limitations}}:
 +
 
 +
* {{tlg|code=yes|nolink=yes|anchor|Foo}} could be linked to with <code><nowiki>[[#Foo|...]]</nowiki></code> from within the same article (let’s call this article “Qux”), or it could be linked to with {{nowrap|<code><nowiki>[[</nowiki>Qux#Foo<nowiki>|...]]</nowiki></code>}} from other articles and from redirects.
 +
 
 +
 
 +
* Anchors can be more suitable for inter‐article linking than section titles are, because anchors are more stable. For example a section title {{nowrap|1=<code>== Foo ==</code>}} within an article titled                      {{code|Qux}}:
 +
:: {{nowrap|<code><nowiki>== {{anchor|Foo bar}} Foo ==</nowiki></code>}}
 +
: Here, links via {{nowrap|<code><nowiki>[[Qux#Foo bar]]</nowiki></code>}} would remain valid even if the section were renamed. A drawback of this approach (as detailed in {{section link||Limitations}}) is that having a template in the section header causes problems with the edit summary window each time that a section edit is done for this section. Note: The anchor is placed before the section name; otherwise browsers may hide the section title from view. Also, the anchor name ({{code|Foo bar}}) should be different than the section ({{code|Foo}}) to avoid invalid HTML.
  
== Examples ==</code>
 
  
(This is actually used on this documentation page, so [[#Examples]], [[#Foo]] and [[#Foo bar]] all work.)
+
* Within section titles, it may be preferable to simply use direct HTML, which may be achieved by substitution like this:
 +
:: {{nowrap|<code><nowiki>== {{subst:anchor|Foo bar}} Foo ==</nowiki></code>}}
 +
: which is saved into the article as:
 +
:: {{nowrap|<code><nowiki>== <span id="Foo bar"></span> Foo ==</nowiki></code>}}
 +
: This provides the stable, linkable anchor, but without the edit problem. The note above still applies. See {{section link||Limitations}} for details.
  
Reasons for the above being best practice are detailed in the following additional, numbered examples, as well as in  [[#Limitations|§&nbsp;Limitations]] below them:
 
  
# <code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}|Foo<nowiki>}}</nowiki></code> <br /> could be linked to with <code><nowiki>[[#Foo|...]]</nowiki></code> from ''within the same article'' (let's call this article "Qux"), <br /> or it could be linked to with {{nowrap|<code><nowiki>[[</nowiki>Qux#Foo<nowiki>|...]]</nowiki></code>}} from ''other'' articles and from redirects (where the ellipsis after the word "Foo" here means the words chosen to represent that link in the Wikipedia article, which, of course, could be anything).
+
* The template can be used to create multiple anchors with a single call. For example:
# Anchors can be more suitable for inter-article linking than section titles are, because anchors are more stable. For example a section title {{nowrap|1=<code>== Foo ==</code>}} within a Wikipedia article titled <code>Qux</code>: <br /> {{nowrap|1=<code style="color:dimgray;"><nowiki>==</nowiki>{{sp}}<code style="color:darkgreen;"><nowiki>{{</nowiki>{{lc:{{BASEPAGENAME}}}}<nowiki>|Foo bar}}</nowiki></code>{{sp}}Foo <nowiki>==</nowiki></code>}} <br /> Here, links via {{nowrap|1=<code><nowiki>[[</nowiki>Qux#Foo bar<nowiki>]]</nowiki></code>}} would remain valid even if the section were renamed {{nowrap|1=<code><nowiki>== {{anchor|Foo bar}} On foo and its many friends ==</nowiki></code>}}<br /> A drawback of this approach (as detailed in [[#Limitations|§&nbsp;Limitations]] below) is that having a template in the section header causes problems with the edit summary window each time that a section edit is done for this section. <br /> {{anchor|Example-note}}{{em|Note:}} The anchor is placed ''before'' the section name; otherwise browsers may hide the section title from view. Also, the anchor name (<code>Foo bar</code>) should be different than the section (<code>Foo</code>) to avoid invalid HTML.
+
:: {{nowrap|<code><nowiki>{{anchor|Foo|Bar|baz}}</nowiki></code>}}
# Within section titles, it may be preferable to simply use direct HTML, which may be achieved by [[WP:substitution|substitution]] like this:<br /> {{nowrap|1=<code style="color:dimgray;"><nowiki>==</nowiki>{{sp}}<code style="color:darkgreen;"><nowiki>{{subst:anchor|Foo bar}}</nowiki></code> Foo <nowiki>==</nowiki></code>}}<br />which is saved into the article as:<br /> {{nowrap|1=<code style="color:dimgray;"><nowiki>==</nowiki>{{sp}}<code style="color:darkgreen;">{{code|1={{anchor|Foo bar}}}}</code> Foo <nowiki>==</nowiki></code>}}<br />This provides the stable, linkable anchor, but without the edit problem.<br /> The [[#Example-note|Note]] above still applies. See [[#Limitations|§&nbsp;Limitations]] below for details.
+
: will create three anchors that can then be linked to with <code><nowiki>[[#Foo]]</nowiki></code>, <code><nowiki>[[#Bar]]</nowiki></code> and <code><nowiki>[[#baz]]</nowiki></code>.
# 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 <code><nowiki>[[#Foo]]</nowiki></code>, <code><nowiki>[[#Bar]]</nowiki></code> and <code><nowiki>[[#baz]]</nowiki></code>.
 
# Here is a more literal example:<br /> Say you wrote an article about a recently discovered Indo-European language called "Yish Yash".  The title of your article would probably be "Yish Yash language" (if there were already an article titled "Yish Yash" that covered the culture generally).  Now let's say that within your article you wished to have a section titled "The placement of the tongue when producing Yish Yash vowel sounds". You would create a section heading that looked like this:<br />{{nowrap|1=<code><nowiki>== The placement of the tongue when producing Yish Yash vowel sounds == </nowiki></code>}}<br />Let's say that you then finished up writing and saved the new article. Now, say you came across another article on Wikipedia titled "Proto-Indo-European vowels" which happened to have a section on the unique tongue placement used in Yish Yash.  You decided it might be useful here for readers to be able to quickly access the section of your article on Yish Yash tongue placement when forming vowels; however, you don't want to send them to the beginning of the whole article on the Yish Yash language (you wanted to target them more specifically).  So you create a link in the Proto-Indo European vowel article to the ''section'' within ''your'' article on the placement of the tongue in Yish Yash: you might do so by editing the text of the Proto-Indo-European vowel article and adding something like this to a relevant place within it:<br />{{nowrap|1=<code><nowiki>[[Yish Yash language#The placement of the tongue when producing Yish Yash vowel sounds|Yish Yash tongue placement]] is particularly interesting.</nowiki></code>}}<br />That is some ''really'' long code, but it would create a direct link to the relevant section of the new Yish Yash language article and would look like this in the article on Proto-Indo-European vowels:<br /> "...<span style="color:blue;">Yish Yash tongue placement</span> is particularly interesting..." <br /> Now let's say another editor came along and wished to change the heading of this section of your article for some legitimate reason—perhaps to "Yish Yash vowels and the tongue".  They could do so by editing your article and making the section heading look like this:<br />{{nowrap|1=<code><nowiki>== Yish Yash vowels and the tongue ==</nowiki></code>}}<br />Unbeknownst to that editor, this would mean the link in the article on Proto-Indo-European vowels that you created earlier would now go by default to the top of the Yish Yash article instead of your carefully-chosen subsection!  However, if an ''anchor'' were placed within the section heading of the Yish Yash article either when you created it or when the other editor came along and changed it—perhaps something like this:<br />{{nowrap|1= <code><nowiki>== {{subst:anchor|Tongue}} The placement of the tongue when producing Yish Yash vowel sounds ==</nowiki></code>}}<br />or like this:<br />{{nowrap|1=<code><nowiki>== {{subst:anchor|Tongue}} Yish Yash vowel–tongue interaction ==</nowiki></code>}}<br />and the text of the article on Proto-Indo-European vowels had been changed by you to something like this:<br />{{nowrap|1=<code><nowiki>[[Yish Yash language#Tongue|Yish Yash tongue placement]] is particularly interesting.</nowiki></code>}}<br />then the link from the Proto-Indo-European article would continue to point to the relevant ''section'' of the Yish Yash article, even if the wording of the section heading there had changed completely and without the reader having to sort it out for themselves.  The use of anchors can make for a cleaner, more efficient Wikipedia reading experience where section headings are changed—which they not infrequently are.
 
  
 
=== Limitations ===
 
=== Limitations ===
  
{|class="wikitable" style="float:right"
+
{|class="wikitable"
!Character
+
! Character
![[Character entity reference|Code]]
+
! Code
!Template
+
! Template
!Meaning
+
! Meaning
 
|-
 
|-
|"
+
| "
|<code>&amp;quot;</code> <br /><code>&amp;#34;</code>
+
| {{code|&amp;quot;}}<br>{{code|&amp;#34;}}
|{{N/a}}
+
|
|(double)&nbsp;[[quotation mark]]
+
| double quotation mark
 
|-
 
|-
|#
+
| #
|<code>&amp;#35;</code>
+
| {{code|&amp;#35;}}
|{{tl|hash}}
+
|
|[[Number sign|hash]]
+
| hash
 
|-
 
|-
|&#124;
+
| &#124;
|<code>&amp;#124;</code>
+
| {{code|&amp;#124;}}
|{{tl|!}}
+
| {{tlg|code=yes|nolink=yes|!}}
|[[Vertical bar|pipe]]
+
| pipe
 
|-
 
|-
|&#61;
+
| &#61;
|<code>&amp;#61;</code>
+
| {{code|&amp;#61;}}
|{{tl|{{=}}}}
+
| {{tlg|code=yes|{{=}}}}
|[[Equals sign|equals]]
+
| equals sign
 
|}
 
|}
* 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 must be unique on a page, and must 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 that contain any character shown in the table will not work as expected. However, any of these characters can be replaced with the codes. The pipe symbol and equals sign can be worked around with {{tlg|code=yes|nolink=yes|!}} and {{tlg|code=yes|nolink=yes|{{=}}}}, 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 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 <code><nowiki>=== {{anchor|Bar}} Baz ===</nowiki></code>, link to it with <code><nowiki>[[pagename#Bar]]</nowiki></code>, not <code><nowiki>[[pagename#bAR]]</nowiki></code>. 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 <code><nowiki>=== {{anchor|Baz}} Abcd ===</nowiki></code> and <code><nowiki>=== {{anchor|bAZ}} Efgh ===</nowiki></code>.
+
 
* If a template is inside a section title then the template code will appear in the edit summary window each time a section edit of that section begins, as in {{nowrap|1="<code><nowiki>/* {{anchor|Issues}}Limitations */ New issue</nowiki></code>"}}. The editor must manually fix the section title part of the edit summary window, or when the section is saved, the browser may not return to the section and the section link of that edit in the history page doesn't work. When using anchor in a section title, consider substituting the template, resulting in <code>{{code|1={{anchor|...}}}}</code> being saved.
+
* Anchor names must be unique on a page, and must 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. They result in invalid HTML.
 +
 
 +
* 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 <code><nowiki>=== {{anchor|Bar}} Baz ===</nowiki></code>, link to it with <code><nowiki>[[pagename#Bar]]</nowiki></code>, not <code><nowiki>[[pagename#bAR]]</nowiki></code>. 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 <code><nowiki>=== {{anchor|Baz}} Abcd ===</nowiki></code> and <code><nowiki>=== {{anchor|bAZ}} Efgh ===</nowiki></code>.
 +
 
 +
* If a template is inside a section title then the template code will appear in the edit summary window each time a section edit of that section begins, as in {{nowrap|"<code><nowiki>/* {{anchor|Issues}}Limitations */ New issue</nowiki></code>"}}. The editor must manually fix the section title part of the edit summary window, or when the section is saved, the browser may not return to the section and the section link of that edit in the history page doesn’t work. When using anchor in a section title, consider substituting the template, resulting in <code><nowiki><span id="..."></span></nowiki></code> being saved.
  
 
=== Use in tables ===
 
=== Use in tables ===
  
Anchors may be used within tables, subject to certain restrictions. The {{tlc|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:
+
Anchors may be used within tables, subject to certain restrictions. The {{tlg|code=yes|nolink=yes|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:
  
 
<syntaxhighlight lang=text>
 
<syntaxhighlight lang=text>
Line 70: Line 87:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
You need to ensure that the {{tlc|anchor}} is not in that portion of the markup intended for the classes, styles etc. Thus, {{tlc|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:
+
You need to ensure that the {{tlg|code=yes|nolink=yes|anchor}} is not in that portion of the markup intended for the classes, styles etc. Thus, {{tlg|code=yes|nolink=yes|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:
  
 
<syntaxhighlight lang=text>
 
<syntaxhighlight lang=text>

Latest revision as of 17:41, 21 July 2020

The template {{anchor}} inserts one or more invisible HTML fragment identifiers (anchor names) in a page. Those locations can then be linked to using [[#Location|...]] syntax. Note that #Location here is not a browser instruction like #REDIRECT—the word “Location” can be any word you wish to associate with any part of an article in which you decide to place the {{anchor}} template, and can be entirely arbitrary; also, the first letter of the location is usually capitalised to reflect the common capitalisation used in section headers.

Usages

{{anchor|Foo|Foo bar}}

== Examples ==

#Examples, #Foo and #Foo bar all work.

Best practices

Reasons for the above being best practice are detailed in the following additional, numbered examples, as well as in § Limitations:

  • {{anchor|Foo}} could be linked to with [[#Foo|...]] from within the same article (let’s call this article “Qux”), or it could be linked to with [[Qux#Foo|...]] from other articles and from redirects.


  • Anchors can be more suitable for inter‐article linking than section titles are, because anchors are more stable. For example a section title == Foo == within an article titled Qux:
== {{anchor|Foo bar}} Foo ==
Here, links via [[Qux#Foo bar]] would remain valid even if the section were renamed. A drawback of this approach (as detailed in § Limitations) is that having a template in the section header causes problems with the edit summary window each time that a section edit is done for this section. Note: The anchor is placed before the section name; otherwise browsers may hide the section title from view. Also, the anchor name (Foo bar) should be different than the section (Foo) to avoid invalid HTML.


  • Within section titles, it may be preferable to simply use direct HTML, which may be achieved by substitution like this:
== {{subst:anchor|Foo bar}} Foo ==
which is saved into the article as:
== <span id="Foo bar"></span> Foo ==
This provides the stable, linkable anchor, but without the edit problem. The note above still applies. See § Limitations for details.


  • 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
" &amp;quot;
&amp;#34;
double quotation mark
# &amp;#35; hash
| &amp;#124; {{!}} pipe
= &amp;#61; {{=}} equals sign
  • Anchor names that contain any character shown in the table will not work as expected. However, any of these characters can be replaced with the codes. The pipe symbol and equals sign can be worked around with {{!}} and {{=}}, respectively. Markup code such as and (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. They result in invalid HTML.
  • 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 === {{anchor|Bar}} Baz ===, 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 === {{anchor|Baz}} Abcd === and === {{anchor|bAZ}} Efgh ===.
  • If a template is inside a section title then the template code will appear in the edit summary window each time a section edit of that section begins, as in "/* {{anchor|Issues}}Limitations */ New issue". The editor must manually fix the section title part of the edit summary window, or when the section is saved, the browser may not return to the section and the section link of that edit in the history page doesn’t work. When using anchor in a section title, consider substituting the template, resulting in <span id="..."></span> being saved.

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.