0

我正在尝试创建一个 mediawiki 模板:

|-
| {{{name}}}{{#if:{{{ref|}}}|<ref>{{{ref}}}</ref>|}}{{#if:{{{ndb|}}}|<ref>https://ndb.nal.usda.gov/ndb/foods/show/{{{ndb}}}</ref>|}} || {{{size|--}}} || {{{carbs|--}}} || {{{sugar|--}}} || {{{fiber|--}}} || {{{fat|--}}} || {{{protein|--}}}

大部分工作正常,但如果我传递一个ref或一个ndb参数,#if我得到的不能正常工作

<ref>{{{ref}}}</ref>

或者

<ref>https://ndb.nal.usda.gov/ndb/foods/show/{{{ndb}}}</ref>

我没有得到我所期望的:

<ref>http://the.passed.value/</ref>

或者

<ref>https://ndb.nal.usda.gov/ndb/foods/show/passed_value</ref>

4

1 回答 1

1

#if似乎不是问题的原因。原因是<ref>未按您期望的顺序处理。您需要#tag在稍后的模板处理阶段生成解析器或扩展标记:

Template:Google源代码:

<includeonly><!--
-->{{#if: {{{ref|}}}
   | {{#tag: ref |{{{ref}}}}}
   }}<!--
-->{{#if: {{{q|}}}
   | {{#tag: ref |[https://google.com/search?q={{#urlencode:{{{q}}}}} Google: {{{q}}}]}}
   }}<!--
--></includeonly>

页面源代码:

{{Google
| ref = http://google.com/search?q=foo+bar
| q = foo bar
}}

<references/>

页面输出:

[1][2]

  1. http://google.com/search?q=foo+bar
  2. 谷歌:富吧
于 2016-05-29T07:40:48.447 回答