MediaWiki模板嵌套调用:参数内斜杠转管道符拆分参数求助
解决MediaWiki模板参数拆分问题
你的问题出在MediaWiki的模板解析顺序上:模板会先按管道符分割参数列表,再解析参数内部的解析器函数(比如#replace)。所以你用#replace把斜杠换成管道符后,结果会被当成一个完整的参数值(比如RS38|4)传给{{Sasines full}}的{{{1}}},而不是分割成两个独立参数。
下面是两种可行的解决方法:
方法1:使用ParserFunctions的拆分函数
如果你的MediaWiki安装了ParserFunctions扩展(绝大多数站点默认安装),可以用#explode(旧版)或#split(新版)直接分割字符串,分别传递两个部分:
{{#if: {{{citation_template|}}} |{{#ifeq:{{{citation_template|}}}|NLS full |* {{NLS full|title={{{nls_title|}}}|id={{{nls_id|}}}|collection={{{nls_collection|}}}|url={{{nls_url|}}}}} |{{#ifeq:{{{citation_template|}}}|Sasines full |* {{Sasines full|{{#explode:{{{nrs_reference|}}}|/|0}}|{{#explode:{{{nrs_reference|}}}|/|1}}}} |* {{#if:{{{citation_template_parameter|}}}|{{{{{citation_template|}}}|{{{citation_template_parameter|}}}={{{citation_template_parameter_value|}}}}} |{{#if:{{{volume|}}}|{{{{{citation_template|}}}|volume={{{volume|}}}}} |{{{{{citation_template|}}}}} }} }} }} }}{{#set: Has citation template=Template:{{{citation_template|}}}}} |{{#if: {{{full_citation|}}}|* {{{full_citation|}}}}} }}
{{#explode:{{{nrs_reference|}}}|/|0}}提取斜杠前的部分(比如RS38){{#explode:{{{nrs_reference|}}}|/|1}}提取斜杠后的部分(比如4)
如果你的MediaWiki版本较新,也可以用#split替代#explode,用法类似:{{#split:{{{nrs_reference|}}}|/|0}}。
方法2:用Lua模块实现更灵活的拆分
如果需要处理更复杂的字符串(比如多个斜杠、空值等),推荐用Lua模块实现:
- 创建
Module:SasinesSplit模块,内容如下:
local p = {} function p.split(frame) -- 获取传入的nrs_reference参数 local ref = frame.args[1] or '' -- 按斜杠分割字符串 local parts = mw.text.split(ref, '/') -- 调用Sasines full模板并传递拆分后的参数 return frame:expandTemplate{ title = 'Sasines full', args = parts } end return p
- 在你的模板中替换对应逻辑:
{{#if: {{{citation_template|}}} |{{#ifeq:{{{citation_template|}}}|NLS full |* {{NLS full|title={{{nls_title|}}}|id={{{nls_id|}}}|collection={{{nls_collection|}}}|url={{{nls_url|}}}}} |{{#ifeq:{{{citation_template|}}}|Sasines full |* {{#invoke:SasinesSplit|split|{{{nrs_reference|}}}}} |* {{#if:{{{citation_template_parameter|}}}|{{{{{citation_template|}}}|{{{citation_template_parameter|}}}={{{citation_template_parameter_value|}}}}} |{{#if:{{{volume|}}}|{{{{{citation_template|}}}|volume={{{volume|}}}}} |{{{{{citation_template|}}}}} }} }} }} }}{{#set: Has citation template=Template:{{{citation_template|}}}}} |{{#if: {{{full_citation|}}}|* {{{full_citation|}}}}} }}
Lua的mw.text.split会自动把分割后的部分作为数组传给模板,完全适配{{Sasines full|{{{1}}}|{{{2}}}}}的参数结构。
内容的提问来源于stack exchange,提问作者redheadkelly
相关产品推荐
相关产品推荐

