Last modified on 3 February 2015, at 08:35

Help:模版

This page is a translated version of the page Help:Templates and the translation is 66% complete.

Other languages:
العربية • ‎بلوچی مکرانی • ‎български • ‎भोजपुरी • ‎català • ‎Deutsch • ‎Zazaki • ‎English • ‎British English • ‎español • ‎فارسی • ‎suomi • ‎français • ‎עברית • ‎Bahasa Indonesia • ‎italiano • ‎日本語 • ‎한국어 • ‎Lëtzebuergesch • ‎Napulitano • ‎Nederlands • ‎polski • ‎پښتو • ‎português • ‎português do Brasil • ‎română • ‎русский • ‎slovenčina • ‎தமிழ் • ‎Türkçe • ‎українська • ‎Tiếng Việt • ‎中文
PD 注意:当你编辑本页面时,你将表示同意以CC0协议授权你的贡献。你可以在公有领域帮助文档找到更多信息。
PD

如果你需要在多个页面上包含同样的文本的话, MediaWiki 的模板功能就起到作用了 (就像上面的帮助文档都会有的标签一样)。 与插件和媒体文件不同的是,模板没有管理中心。编辑者可以从头编写、复制现有模板或导入来自其他维基的模板页面。

创建模板Edit

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

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

您好!歡迎來到維基!

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

{{Welcome}}

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

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

嗨,您好!歡迎來到這個奇妙的維基。

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

这只是模板的一个小应用。除此之外,模板还具有更多灵活易用的功能。

使用模板Edit

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

  • {{Name}} — as described above, this link will be dynamically replaced by the current content of [[Template:Name]] at the time the page with the template link is loaded. The link will remain unchanged in the page's source.
  • {{subst:Name}} — when this template link is used, it will be replaced once and for all with the content of [[Template:Name]] as of the time the page with the link is saved: a copy of the contents of [[Template:Name]] will be substituted for the template link. The contents are then a part of the including page, and can be edited normally, separately from the original. Note: changes to the source template page will not be propagated to the page with the template link.
  • {{safesubst:Name}} — this was introduced to allow for substitution that doesn't break transclusion, see w:en:Help:Substitution#The safesubst: modifier.
  • {{msgnw:模板名称}} 会将模板的源代码包含在页面中,类似<nowiki>一样。

事實上,在一個普通的維基的頁面也可以使用模板,只要指定它在命名空間:

  • {{Template:頁面名稱}} 包含 [[Template:頁面名稱]]
  • {{Foo:頁面名稱}} 包含 [[Foo:頁面名稱]]
  • {{:頁面名稱}} 包含 [[頁面名稱]]
    • {{subst::頁面名稱}} 把它的內容替換成 [[頁面名稱]]

若沒有存在這種空間,否則整個標題會被假定成模板的名稱:

  • {{Foo:欄位}} 包含 [[Template:Foo:欄位]]

在模板中使用变量Edit

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.

假设你希望在其他用户的讨论页插入一条感谢函,例如:

Example sunflower image
感谢你!
感谢你的努力。
来自 张三


感谢函包括感谢的原因(示例中的“你的努力”)和署名(“张三”)。

为了让任何人可以因为任何原因感谢任何人,你需要创建一个感谢函模板,如Template:Thankyou。使用感谢函模板会让所有感谢函看起来相似,但其中指定部分内容应该可以自定义(如感谢的原因和署名),所以你需要把这些内容设置为变量。感谢函模板的核心内容如下所示:

'''感谢你!'''
感谢 {{{1}}}。
来自 {{{2}}}

请注意{{{1}}}{{{2}}},这就是变量,它们可以随着模板引入其他页面。声明变量的方法是使用三组花括号:{{{ }}}

在调用模板时,需要为变量赋值,变量间用一个竖线符号(|)分隔。MediaWiki允许三种为变量赋值的方式:隐式赋值、按编号赋值、按名称赋值。

隐式赋值Edit

隐式赋值要求按变量声明顺序赋值:

{{Thankyou|你的努力|张三}}

例中{{Thankyou}}模板接收{{{1}}}=你的努力<code><nowiki>和<code><nowiki>{{{2}}}=张三,输出:

Example sunflower image
感谢你!
感谢你的努力。
来自 张三


隐式赋值时,赋值的顺序十分重要。假如颠倒了赋值顺序:

{{Thankyou|张三|你的努力}}

输出结果为:

Example sunflower image
感谢你!
感谢张三。
来自 你的努力


注意:若想为变量隐式赋值,则该模板下所有变量必须全部隐式赋值。任何一个变量使用编号或名称赋值后,该模板便不再接受隐式赋值。

Note: If an equal sign appears inside an anonymous template parameter, it may be interpreted as a named parameter, treating the text before the equal sign as the parameter name and the text after it as the value. This is a common problem when you need to include an external link, or an HTML element with attributes (see Task 16235) The workaround is to use named parameters instead, or even numbered parameters as explained in the following section.

按编号赋值Edit

使用编号赋值时,需要写出变量的编号:

{{Thankyou|2=张三|1=你的友好}}

例中,虽然不是按变量的声明顺序赋值,但模板{{Thankyou}}接收到的是{{{1}}}=你的友好{{{2}}}=张三,输出结果为:

Example sunflower image
感谢你!
感谢你的友好。
来自 张三


按名称赋值Edit

与按编号赋值类似,第三种赋值方法只是把编号换成了名称。为了演示,需要将模板内容改为:

'''感谢你!'''
感谢{{{原因}}}。
来自 {{{署名}}}

例中用{{{原因}}}{{{署名}}}而不是数字声明了变量。赋值方法如下:

{{Thankyou|署名=张三|原因=你的一切}}

例中{{Thankyou}}模板接收{{{原因}}}=你的一切{{{署名}}}=张三并输出:

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.

缺省值Edit

当你调用了一个模板却没有为变量赋值:

{{Thankyou}}

以用编号声明变量的模板为例,输出结果为:

Example sunflower image
感谢你!
感谢{{{1}}}。
来自 {{{2}}}


没有为变量赋值的话,会直接输出变量本身。此时,设置变量的缺省值会方便许多。更改模板内容为:

'''感谢你!'''
感谢{{{原因|一切}}}。
来自 {{{署名|张三}}}

{{{原因|一切}}}表示如果{{{原因}}}变量没有被赋值,则输出缺省值一切。类似的,{{{署名|张三}}}{{{署名}}}变量缺省输出张三。修改后的模板输出如下结果:

Example sunflower image
感谢你!
感谢一切。
来自 张三


控制模板嵌入Edit

你可以用<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

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

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 anotherEdit

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 codeEdit

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. If it doesn't work also check for red links listed under "Pages transcluded onto the current version of this page:", below the edit box. If there are any repeat the above steps for these as well.

After sucessfully importing the template and all its linked templates from the other wiki, edit it to change customisations to suit your wiki. For example to change a logo, remove redundant categories or red links.

ExtensionsEdit

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 codeEdit

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 those classes to "MediaWiki:Common.css" on the new wiki and check if the template is now fine.
  2. 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