Help:Templates/zh

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

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

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

Hello! Welcome to the wiki.

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

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

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

Hi there! Welcome to this wonderful wiki.

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

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

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


 * 会将模板的当前内容包含在页面内.
 * 会在保存后将模板的内容以平常可以编辑的方式 (wiki 代码) 插入页面源代码中.
 * — this was introduced to allow for substitution that doesn't break transclusion, see w:en:Help:Substitution.
 * 会将模板的源代码包含在页面中，类似 一样.

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


 * includes
 * includes
 * includes
 * replaces itself with the contents of

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


 * includes

在模板中使用变量
MediaWiki允许在模板中使用变量.

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

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

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

感谢你！ 感谢. 来自

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

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

隐式调用
隐式调用要求按变量声明顺序调用：

In this case, the  template receives parameters   and , producing:

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

would produce this result:

Note: identifying parameters by order (with, 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.

用数字编号的变量
To pass in parameters by number, identify each parameter when passing it:

This time, template  receives parameters   and , though they have been supplied in inverse order, and produces:

用名称的变量
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. hugs,

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

In this case, template  receives parameters   and   and produces:

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
If you transclude a template that expects parameters, but do not provide them, in this way:

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

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. hugs,

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

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

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


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

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


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