For faster navigation, this Iframe is preloading the Wikiwand page for 帮助:替换引用.

帮助:替换引用

这是元维基上帮助文档副本的中文翻译,请参看元维基上的帮助文档后再修改本文。

本文在元维基的原文是:meta:Help:Substitution

修饰符“subst:”(“substitute”,“替代”)位于模板、变量、解析函数的“((”之后。但subst对参数的“(({”不起作用。

它能在引用页面保存时,将维基代码自动替换,分别为:

  • 模板的维基代码,而不是引用模板的维基代码(例如((template1))),参数被替换成参数的值
  • 变量的值
  • 解析函数的值

对模板的替换引用不会显示在页面历史和模板的链入页面等中。

模板[编辑]

样例: Template:t2(讨论 - 链入页面 - 编辑),包含<noinclude>((Sample))[[Category:辅助模板]]</noinclude>start_(({1))}_middle_(({2))}_end,以((subst:t2|[[a]]|((tc))))方式引用,得到原始码start_[[a]]_middle_((tc))_end,显示为start_a_middle_((tlc|...))_end。

注意到这种对模板的替换不会自动重复进行:如果被替换引用的模板又引用了其他模板,那个模板不会被替换引用。你可以随后加上subst,做手动转换。这种逐步的替换引用对于分析模板调用原理很有用。但是,复杂的是:

  • 如果参数名取决于另一个参数的默认值,那么替换引用时这不会被计算。

如果你想修改替换引用生成的目标代码,你必须先保存,因为预览时替换引用还没发生,替换引用只在保存时发生。但是你可以点击显示差异以显示替换引用的结果。

另一种方式就是手动替换,复制模板的原始码再编辑。另一种方式是使用“msgnw:”修饰符(参见:Wikipedia:MediaWiki命名空间#MSGNW)。

对“~~~~”进行替换引用没有效果。

变量[编辑]

对变量替换引用与对模板的类似。比如时间戳:

((subst:CURRENTYEAR))年((subst:CURRENTMONTHNAME))((subst:CURRENTDAY))日, ((subst:CURRENTTIME)) (UTC)

得到:

2024年7月2日, 04:16 (UTC)

解析函数[编辑]

对解析函数的替换引用与模板一样,但是“subst:”与“#”之间不能有分隔。例如((subst:#time:c))直接得到2024-07-02T04:16:29+00:00,而((subst: #time:c))得到((subst: #time:c))

nowiki与pre[编辑]

如果某模板包含位于nowiki或pre标记之内的签名或替换引用,他们将被解释并展开,但nowiki或pre标记仍保留。

条件替换技术[编辑]

是否进行替换可由特定参数决定,由此还可进行多重替换。原理如下:

  • 页面A包含((subst:B|subst=subst:|参数...))
  • template:B包含(((({subst|))}C|subst=(({subst|))}|参数...))
  • template:C包含(((({subst|))}D|subst=(({subst|))}|参数...))
  • 以此类推

当页面A对template:B的引用包含“subst=subst:”时,template:B对template:C的引用也是替换引用,template:C对template:D也是。若页面A不含“subst=subst:”时,便不会替换,且不会出现错误。由此,条件替换和多重替换可以实现。

基于引用的替换[编辑]

在被替换的模板中使用“仅包含”和“不包含”标签,可以在引用时实现魔术般的技巧。

代码~<includeonly>~</includeonly>~~显示为~~~,当被引用时显示为~~~~,当被替换引用时展开为用户签名。而当一包含了((<includeonly>subst:</includeonly>CURRENTTIME))的模板被替换引用并保存时,不变的时间就被保存在维基文字中,替换了随时间变化的时间变量。变量也是如此。样例:

在Template:Demo中保存((CURRENTTIME)),源文件为((CURRENTTIME))
  • 在页面中保存((Demo)),源文件为((Demo)),页面显示出变化的当前时间04:16;
  • 在页面中保存((subst:Demo)),源文件为((CURRENTTIME)),页面显示出变化的当前时间04:16。
在Template:Demo中保存((subst:CURRENTTIME)),源文件为07:37
  • 在页面中保存((Demo)),源文件为((Demo)),页面显示出不变的模板保存时间07:37;
  • 在页面中保存((subst:Demo)),源文件为07:37,页面显示出不变的模板保存时间07:37。
在Template:Demo中保存((<includeonly>subst:</includeonly>CURRENTTIME)),源文件为((<includeonly>subst:</includeonly>CURRENTTIME))
  • 在页面中保存((Demo)),源文件为((Demo)),页面显示出((subst:CURRENTTIME));
  • 在页面中保存((subst:Demo)),源文件为07:37,页面显示出不变的页面保存时间07:37。
在Template:Demo中保存(( (({|safesubst:))}CURRENTTIME)),源文件为(( (({|safesubst:))}CURRENTTIME))}
  • 在页面中保存((Demo)),源文件为((Demo)),页面显示出变化的当前时间04:16;
  • 在页面中保存((subst:Demo)),源文件为07:37,页面显示出不变的页面保存时间07:37。

简单的说:仅包含标签可被用于专门被替换引用的模板、变量,即在subst:前后加上仅包含标签。

使用限制[编辑]

模板内部如果嵌入了引用替换方式的其它模板或者有引用替换效果的签名~~~、~~~~、~~~~~时,模板自身也只能以引用替换的方式使用,即以((subst:模板名|相关参数))的方式来使用模板。比如模板A里包含了

這是一個關於((<includeonly>subst:</includeonly>B))的例子

那么,只能这样使用模板A:

((subst:A))

其他修饰符[编辑]

msgnw

自动产生模板的原始码。此修饰符能与subst或safesubst叠加使用。如((subst:msgnw:foo))((safesubst:msgnw:foo))

subst:msgnw:

这是一种替换引用的变种,能将模板原封不动地替换到目标页面。一般替换引用会同时替换模板参数的值、变量的值和解析函数的值,但((subst:msgnw:模板名稱))的组合会导致模板内容连同包含<nowiki><noinclude><includeonly><onlyinclude>等代码原封不动地替换引用到目标页面,是一种WP:剪贴移动或拷贝条目的可能做法,不过目前中文维基不允许剪贴移动,但此用法也为沙盒模板的拷贝提供了一种替换引用方案。

safesubst

修饰符“subst:”有可能会因故导致失败,而若失败时,修饰符“subst:”会保持未解析的状态(形如((subst:模板名稱)))而导致模板功能出现问题。若出现此种情况应该将修饰符“subst:”改为“safesubst:”修饰符,此时若替换引用发生错误则会取消替换引用变为普通的模板引用效果,来确保其他相关模板运作状况正常。此功能通常用于模板编程时,将模板内部的替换引用使用“safesubst:”修饰符,以令模板替换引用无论正常与否都能保持相同的输出。

参见[编辑]

{{bottomLinkPreText}} {{bottomLinkText}}
帮助:替换引用
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?