User:Nihiltres/Template syntax style
This is an essay. It contains the advice or opinions of one or more Wikipedia contributors. This page is not an encyclopedia article, nor is it one of Wikipedia's policies or guidelines, as it has not been thoroughly vetted by the community. Some essays represent widespread norms; others only represent minority viewpoints. |
This is a short guide on how template syntax probably ought to be best laid out for readability. It's an obscenely minor issue, that, as with regional varieties of English, shouldn't ever be fought over; it isn't worth the time. That said, it's nice to be consistent, so this guide lays out a set of three acceptable, clean styles, and when they ought to be preferred.
Compact style
[edit]The compact style is very simple: it omits all unnecessary whitespace. It lends itself best to short template calls. It should usually be preferred when a template call includes unnamed parameters. It is also appropriate for template calls with exactly one parameter that is a named parameter, so long as that parameter is relatively short. If a parameter is very long, consider using the block style instead. For example:
{{convert|10|cm|in|abbr=on}}
Or:
{{Unreferenced|date=June 2015}}
Inline style
[edit]The inline style uses spaces to separate its parameters visually, improving readability for long inline template calls. It should be used for all inline template calls that do not use the compact style.
There should be exactly one space inserted before each pipe character, and no spaces between the pipe, parameter name, equals sign, or the start of the parameter value. There is no extra space after the end of a parameter value; this is redundant to the space before each pipe. For example:
{{cite book |author=Dr. Seuss |title=Fox in Socks |date=1965 |publisher=Beginner Books |publicationplace=New York |page=44 |oclc=304375 |isbn=978-0-329-12661-2}}
Block style
[edit]The block style uses both spaces and linebreaks to separate its parameters visually. It is the clearest template layout, but it is bulky and impractical for use in the middle of a paragraph. Therefore, it should be used only outside of paragraphs, where it would break their visual flow. For example, it is appropriate for infoboxes and lists.
The template name, template closure, and each individual parameter each are given their own line. Each parameter line should start with a pipe followed by a space, then the parameter name, a variable number of spaces, an equals sign, exactly one space, and then the parameter value. The variable number of spaces before the equals sign should be normalized so that each parameter line has an equal number of characters before its equals sign, and so that there is exactly one space between the longest parameter name and the equals sign. This lines up the template nicely when the edit field uses a monospace font. There should be no trailing whitespace on any line unless a parameter is empty, in which case one space following the equals sign is acceptable. There should not be any spaces or pipes before the template closure. For example:
{{Infobox artwork | title = The Chandos Portrait<br>of William Shakespeare | image_file = Shakespeare.jpg | image_size = 250px | type = Oil on canvas | artist = Attributed to [[John Taylor (painter)|John Taylor]] | year = {{circa|1600s}} | height_metric = 55.2 | width_metric = 43.8 | height_imperial = {{frac|21|3|4}} | width_imperial = {{frac|17|1|4}} | metric_unit = cm | imperial_unit = in | city = London | museum = [[National Portrait Gallery, London|National Portrait Gallery]] | accession = NPG 1 | italic title = no }}
Handling of nested templates
[edit]Sometimes templates are called inside of other template calls, e.g. citation templates in infobox fields. When templates that use the inline or block formats are nested in a template using the block format, they should also use the block format, with each parameter line, and the closing braces, indented by four spaces. For example:
{{infobox example | title = An example infobox | image = Example.png | caption = Seriously, look at this example. | some_parameter = Text with citation<ref>{{cite book |author = Dr. Seuss |title = Fox in Socks |date = 1965 |publisher = Beginner Books |location = New York |page = 44 |oclc = 304375 |isbn = 978-0-329-12661-2 }}</ref> | some_other_parameter = {{plainlist| * List item 1 * List item 2 * List item 3 }} }}
Note that certain nested templates may require forms that require input to be placed at the beginning of a new line, e.g. the list formatting of {{plainlist}} input. In this case, indent consistently where possible. Even if it's inconsistent, if possible, indent the closing braces of a block-form nested template to distinguish them from the closing braces of the parent template.
Variant: paired parameters
[edit]Sometimes two or more parameters work closely together, e.g. the lastn
and firstn
parameters for author names. In that case, it's acceptable to place them on the same line for compactness, separated by a space instead of a newline. Both parameters on the same line should ignore the unified position for the equals sign, instead using the spacing style preferred by the inline style, except with a single space between the pipe and the start of the parameter name. For example:
{{cite book | last=Miéville | first=China | date = 2002 | title = The Scar | publisher = [[Macmillan Publishers]] | isbn = 978-0-333-78174-6 }}
Note that {{cite book}} should usually use the inline style except in lists of sources.
Variant: unnamed parameters
[edit]In the event that a template has one or more unnamed parameters that are very long, confusing, or span multiple lines, it's acceptable to use a variant block style, by replacing the spaces of the horizontal style with linebreaks. For example, the inline style example in this essay is formatted with a template like so:
{{Pre |<nowiki>{{cite book |author=Dr. Seuss |title=Fox in Socks |date=1965 |publisher=Beginner Books |publicationplace=New York |page=44 |oclc=304375 |isbn=978-0-329-12661-2}}</nowiki> }}