Help:模版

From MediaWiki.org
Jump to: navigation, search
This page is a translated version of the page Help:Templates and the translation is 31% complete.

Other languages:
العربية 7% • ‎български 5% • ‎català 100% • ‎Deutsch 42% • ‎Zazaki 7% • ‎English 100% • ‎British English 94% • ‎español 29% • ‎suomi 20% • ‎français 97% • ‎Bahasa Indonesia 11% • ‎italiano 25% • ‎日本語 32% • ‎한국어 27% • ‎Nederlands 7% • ‎polski 12% • ‎پښتو 1% • ‎português 83% • ‎português do Brasil 17% • ‎română 22% • ‎русский 31% • ‎slovenčina 26% • ‎தமிழ் 2% • ‎Türkçe 4% • ‎Tiếng Việt 22% • ‎中文 31%
PD 注意:当你编辑本页面时,你将表示同意以CC0协议授权你的贡献。你可以在公有领域帮助文档找到更多信息。
PD

如果你需要在多个页面上包含同样的文本的话, MediaWiki 的模板功能就起到作用了 (就像上面的帮助文档都会有的标签一样)。

创建模板[edit | edit source]

模板其实也是一种 wiki 页面,但它是主要是用于嵌入到其它页面中。模板的页面名称最前面都有 Template:——將它分配到該命名空間。除此之外,像其他页面一样创建就可以了。

下面展示了模板的最簡單使用方法。如果你以如下一段爲內容,創建了一個名叫“Template:Welcome”的模板

Hello! Welcome to the wiki.

這樣,你就創建了你的第一個模板!如果你接下來插入:

{{Welcome}}

到其他任一頁面,當該頁面被訪問時,“Hello! Welcome to the wiki”字樣就會代替{{Welcome}}而顯示。模板的內容會嵌入到其他頁面,例如:它已整合到剛剛的頁面。

你可以在任一頁面的任何位置添加{{Welcome}} 來歡迎其他人。設想一下該模板被用在100個頁面中。如果你後來將模板內容變成:

Hi there! Welcome to this wonderful wiki.

並且重新訪問之前用到該模板的的100個頁面,你會看到新的文字而不是原先的。你用這個方法改變了100個頁面的內容,而不用手動修改它們,因爲模板是被嵌入這些頁面中的。

This is the basic mechanism. There are several additional features of transclusion that enrich this mechanism and make templates very useful.

使用模板[edit | edit source]

模板除了作为普通的 wiki 页面之外,还可以有以下三种使用方法:

  • {{模板名称}} 会将模板的当前内容包含在页面内。
  • {{subst:模板名称}} 会在保存后将模板的内容以平常可以编辑的方式 (wiki 代码) 插入页面源代码中。
  • {{safesubst:Name}} — this was introduced to allow for substitution that doesn't break transclusion, see w:en:Help:Substitution#The safesubst: modifier.
  • {{msgnw:模板名称}} 会将模板的源代码包含在页面中,类似<nowiki>一样。

In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace it resides in, so:

  • {{Template:Pagename}} includes [[Template:Pagename]]
  • {{Foo:Pagename}} includes [[Foo:Pagename]]
  • {{:Pagename}} includes [[Pagename]]
    • {{subst::Pagename}} replaces itself with the contents of [[Pagename]]

If no such namespace exists, the full title is assumed to be a template:

  • {{Foo:Bar}} includes [[Template:Foo:Bar]]

在模板中使用变量[edit | edit source]

To enrich the mechanism of transclusion, MediaWiki allows parameters to be passed to a template when it is transcluded. Parameters allow the template to produce different contents or have different behaviors.

Suppose you wish to insert a little thank you note in the talk page of other users, such as:

Example sunflower image
A little thank you...
for all your effort.
hugs, Me


The thank you note will have a reason (in this case, "all your effort") and a signature ("Me"). Your objective is that any user is able to thank any other user, for any reason whatsoever.

So that the note will look similar everywhere it is used, you can define a template called Template:Thankyou, for example. Although the note should look similar whenever a user thanks another user, its specific contents (i.e. the reason and the signature) will be different. For that reason, you should pass them as parameters. If we ignore the remaining elements to format the box and place the image, the core content of the template will be this:

'''A little thank you...'''
for {{{1}}}.
hugs, {{{2}}}

Notice the use of {{{1}}} and {{{2}}}. This is the way to identify, within templates, the parameters that will be passed in when the template is used. Note that, within the template, each parameter is surrounded by three braces: {{{ }}}. This is different from normal template name usage.

When using the template on a page, you fill in the parameter values, separated by a "pipe" character (|). MediaWiki allows parameters to be passed to the template in three ways: Anonymously, Numbered, and Named.

Anonymous parameters[edit | edit source]

To pass in anonymous parameters, list the values of those parameters sequentially:

{{Thankyou|all your effort|Me}}

In this case, the {{Thankyou}} template receives parameters {{{1}}}=all your effort and {{{2}}}=Me, producing:

Example sunflower image
A little thank you...
for all your effort.
hugs, Me


The order in which anonymous parameters are passed in is crucial to its behavior. Reversing the order of the parameters, like so:

{{Thankyou|Me|all your effort}}

would produce this result:

Example sunflower image
A little thank you...
for Me.
hugs, all your effort


Note: identifying parameters by order (with {{{1}}}, etc) works only with anonymous parameters. If your page identifies any parameter by number or name, as shown below, this method will no longer be available to the template which receives them.

用数字编号的变量[edit | edit source]

To pass in parameters by number, identify each parameter when passing it:

{{Thankyou|2=Me|1=your friendship}}

This time, template {{Thankyou}} receives parameters {{{1}}}=your friendship and {{{2}}}=Me, though they have been supplied in inverse order, and produces:

Example sunflower image
A little thank you...
for your friendship.
hugs, Me


用名称的变量[edit | edit source]

The third way of passing parameters is by name, instead of numbers. In this case, the template contents would be changed to:

'''A little thank you...'''
for {{{reason}}}.
hugs, {{{signature}}}

Within the template, we use {{{reason}}} and {{{signature}}} to identify each parameter, instead of a number. To pass these parameters by name, identify each parameter when passing it:

{{Thankyou|signature=Me|reason=being who you are}}

In this case, template {{Thankyou}} receives parameters {{{reason}}}=being who you are and {{{signature}}}=Me and produces:

Example sunflower image
A little thank you...
for being who you are.
hugs, Me

The advantage of using named parameters in your template, besides also being flexible in the order parameters can be passed, is that it makes the template code much easier to understand if there are many parameters.

Default values[edit | edit source]

If you transclude a template that expects parameters, but do not provide them, in this way:

{{Thankyou}}

in the numbered parameters example above you would get the following:

Example sunflower image
A little thank you...
for {{{1}}}.
hugs, {{{2}}}


Since no parameters were passed in, the template presents the parameters themselves, instead of their respective values. In these cases, it may be useful to define default values for the parameters, i.e. values that will be used if no value is passed in. For example, if the template contents are changed to:

'''A little thank you...'''
for {{{reason|everything}}}.
hugs, {{{signature|Me}}}

then {{{reason|everything}}} defines that if no parameter {{{reason}}} is provided, then the value everything will be used. Similarly, {{{signature|Me}}}, defaults parameter {{{signature}}} to value Me. Now, transcluding the template again without passing any parameter, results in the following:

Example sunflower image
A little thank you...
for everything.
hugs, Me


控制模板嵌入[edit | edit source]

你可以用<noinclude><includeonly> 来控制模板的哪些部分会被嵌入。

任何在<noinclude></noinclude>之间的标记,都只会在直接查看模板页面的时候显示出来,而不会被包含在页面中。可能的使用方式有:

  • 为模板分类
  • 连接到其他语言版本中的类似模板的跨语言链接
  • 关于如何使用该模板的解释文字

与此相对的是<includeonly>。在<includeonly></includeonly>之间的文字只会在嵌入页面的时候才会被处理或显示。

  • 很明显的一个使用方式是将所有有某个模板的页面加入某个分类,而不将模板本身加入这个分类。Note 注意: 当你改变模板中的分类的时候,使用这个模板的分类可能不会立即改变;这会在工作排队中处理。
  • Ensuring that the template's code is not executed when viewing the template page itself. Typically this is because it expects parameters, and its execution without parameters has an undesired result.

Everything outside <noinclude> and <includeonly> tags is processed and displayed normally; that is, both when the template page is being viewed directly and when the template is included in another page.

整理模板[edit | edit source]

为了使模板起作用,用户需要找到并且了解如何使用模板。一个简单的技巧是在模板页面中插入一个例子。

To find them, users can:

  1. Click Special pages > All pages
  2. In the Namespace: list, choose Template and click Go.

例如:

<noinclude>
== 用法 ==
用这个模板来初始化水属性魔法:
{{模板名称|关键字1+关键字2+关键字3}}
</noinclude>

然后,编辑者就可以剪切粘贴来插入这个模板。

Copying from one wiki to another[edit | edit source]

Templates often require CSS or other templates, so users frequently have trouble copying templates from one wiki to another. The steps below should work for most templates.

MediaWiki code[edit | edit source]

If you have import rights (Specifically importupload) on the new wiki:

  1. Go to Special:Export on the original wiki, and download an .xml file with the complete history of all necessary templates, as follows:
    • Enter the name of the template in the big text box, e.g. "Template:Welcome". Pay special attention to capitalization and special characters — if the template name isn't exactly correct, the export may still occur but the .xml file will not have the expected data.
    • Check the box "Include templates".
    • Uncheck the box "Include only the current revision, not the full history".
    • Click "Export".
  2. Go to Special:Import on the new wiki and upload the .xml file.

If you don't have import rights on the new wiki:

  1. Go to the template you want to copy from the original wiki. Go to the edit page, and copy all the wikitext
  2. On the new wiki, go to the page with the same name as the template you copied. Hit create/edit and paste the wikitext you copied. In the edit summary of each template, link to the original page for attribution.
  3. Back in the original wiki at the edit window, below the edit box, look at the list of "Templates used on this page". For each template listed follow these instructions. Also do that for any template used by any of these templates, and so on.

This will copy the entire code necessary, and will suffice for some templates.

Extensions[edit | edit source]

An extension often used in templates is ParserFunctions. Visit page ParserFunctions and check if any of the functions listed there are used in the templates you've copied. If so, you have to install the ParserFunctions extension. To install it, you'll need system admin access to the server of your MediaWiki installation.

Another dependency that may be used in templates, especially those on Wikipedia, is Lua. Having {{#invoke: }} in template code is a good sign for it. In case it's used, you need to install the Scribunto extension and system admin access is required too. See that page for more instructions about installing and using the extension.

CSS and JavaScript code[edit | edit source]

Besides MediaWiki code, many templates make use of CSS and some rely on JavaScript to work fully. If the copied templates are not behaving as expected, this may be the cause. To copy the required CSS and JavaScript to your wiki you'll normally need to have admin privileges, because you'll be editing system messages in the "MediaWiki:" namespace.

  1. Look for the use of CSS classes (text like class="foobar") in the template text. If those classes appear in "MediaWiki:Common.css" or "MediaWiki:Monobook.css" on the original wiki, copy them to "MediaWiki:Common.css" on the new wiki and check if the template is now fine.
  1. If the copied template is still not working as expected, check if there is code in "MediaWiki:Common.js" or "MediaWiki:Monobook.js" on the original wiki. If so, you can try copying it to "MediaWiki:Common.js" on the new wiki. Normally, it is a good idea to only copy code from trusted sources, and first browsing the code to identify and select the parts that are relevant. You may find comments that can serve as clues to identify the functionality of each part.

参见[edit | edit source]