Help:Templates/zh

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

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

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

您好!歡迎來到維基!

這樣，你就創建了你的第一個模板！如果你接下來插入：

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

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

嗨，您好！歡迎來到這個奇妙的維基.

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

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

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


 * — 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.
 * — 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.
 * — this was introduced to allow for substitution that doesn't break transclusion, see w:en:Help:Substitution.
 * 会将模板的源代码包含在页面中，类似 一样.

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


 * 包含
 * 包含
 * 包含
 * 把它的內容替換成

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


 * 包含

在模板中使用变量
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.

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

感谢函包括感谢的原因（示例中的“你的努力”）和署名（“张三”）.

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

感谢你！ 感谢. 来自

请注意 和 ，这就是变量，它们可以随着模板引入其他页面. 声明变量的方法是使用三组花括号：.

在调用模板时，需要为变量赋值，变量间用一个竖线符号（ ）分隔. MediaWiki允许三种为变量赋值的方式：隐式赋值、按编号赋值、按名称赋值.

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

例中 模板接收 ，输出：

隐式赋值时，赋值的顺序十分重要. 假如颠倒了赋值顺序：

输出结果为：

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

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.

按编号赋值
使用编号赋值时，需要写出变量的编号：

例中，虽然不是按变量的声明顺序赋值，但模板 接收到的是 和 ，输出结果为：

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

感谢你！ 感谢. 来自

例中用 和 而不是数字声明了变量. 赋值方法如下：

例中 模板接收 和 并输出：

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.

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

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

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

感谢你！ 感谢. 来自

表示如果 变量没有被赋值，则输出缺省值. 类似的， 中 变量缺省输出. 修改后的模板输出如下结果：

控制模板嵌入
你可以用 和   来控制模板的哪些部分会被嵌入.

任何在 和  之间的标记，都只会在直接查看模板页面的时候显示出来，而不会被包含在页面中. 可能的使用方式有：


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

与此相对的是. 在 和  之间的文字只会在嵌入页面的时候才会被处理或显示.


 * 很明显的一个使用方式是将所有有某个模板的页面加入某个分类，而不将模板本身加入这个分类. 当你改变模板中的分类的时候，使用这个模板的分类可能不会立即改变；这会在中处理.
 * 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  and   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.

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

To find them, users can:
 * 1) Click ' > '
 * 2) In the ' list, choose Template and click '.

例如：

用法
用这个模板来初始化水属性魔法：

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

Copying from one wiki to another
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
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:
 * 2) * 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.
 * 3) * Check the box "".
 * 4) * Uncheck the box "".
 * 5) * Click "".
 * 6) 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.

Extensions
An extension often used in templates is ParserFunctions. Visit page and check if any of the functions listed there are used in the templates you've copied. If so, you have to install the. 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  in template code is a good sign for it. In case it's used, you need to install the and system admin access is required too. See that page for more instructions about installing and using the extension.

CSS and JavaScript code
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  ) 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.

参见

 * – a template special use case example
 * – 可能在某些模板中找到的花哨的东西
 * m:Help:Template – contains a much more thorough manual on how exactly templates function
 * m:Help:Advanced templates – describes even more advanced techniques such as dynamic template calls and variable variables
 * m:Help:Embed page – embedding pages from namespaces other than.
 * Importing Wikipedia infoboxes tutorial
 * m:Help:Embed page – embedding pages from namespaces other than.
 * Importing Wikipedia infoboxes tutorial