跳转到内容

模板:If then show

来自天灾之下:危险连接资料站
模板文档

This template tests whether the first unnamed parameter is not an empty string and returns it if it is not empty. Otherwise it returns the second unnamed parameter.

This is useful when the first parameter is a {{#invoke: }} of a Lua module that may return a value or nothing. Because it uses the {{#invoke: }} just once, it simplifies the coding of infoboxes, for example.

The optional third and fourth unnamed parameters provide a prefix and a suffix for the first parameter when returned. This enables the designer to construct more complex templates that still only use one invoke of a Lua module.

Usage

模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (P26) for 模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (Q151973)
{{If then show|{{#invoke:WikidataIB|getValue|P26|qid=Q151973|fwd=ALL|osd=no}}|unmarried}} → package.lua第80行Lua错误:module 'Module:i18n' not found
模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (P26) for 模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (Q905)
{{If then show|{{#invoke:WikidataIB|getValue|P26|qid=Q905|fwd=ALL|osd=no}}|unmarried}} → package.lua第80行Lua错误:module 'Module:i18n' not found

Prefix and suffix

Infobox field with microformat

A value in an infobox can be designed to be wrapped in a microformat if the wikidata call is not empty:

{{if then show | {{#invoke:WikidataIB | getValue | rank=best |P19 |name=birth_place |qid={{{qid|}}} |suppressfields={{{suppressfields|}}} |fetchwikidata={{{fetchwikidata|}}} |noicon={{{noicon|}}} | {{{birth_place|}}} }} || <span class="birthplace"> | </span>}}
If there is a local parameter supplied in the article (e.g. |birth_place=London), the code will return <span class="birthplace">London</span>.
If there is no local parameter it will attempt to fetch a value for 模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (P19) from Wikidata.
If that has a value, such as "New York", the code will return <span class="birthplace">New York</span>.
If there is no value returned from Wikidata, the code returns nothing.

Table

A table can be designed to display a row only when a particular property of the article exists on Wikidata.

模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (P26) for 模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (Q151973)
{| class="wikitable"
|-
! scope="col" style="width:15em;" | Label
! scope="col" style="width:36em;" | Value
{{If then show|{{#invoke:WikidataIB|getValue|P26|qid=Q151973|fwd=ALL|osd=no}}||<tr><th>{{#invoke:WikidataIB|getLabel|P26|qid=Q151973}}</th><td>|</td></tr>}}
|}
→
Label Value
package.lua第80行Lua错误:module 'Module:i18n' not foundpackage.lua第80行Lua错误:module 'Module:i18n' not found
模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (P26) for 模块:Wikidata_label第271行Lua错误:attempt to index field 'wikibase' (a nil value) (Q905)
{| class="wikitable"
|-
! scope="col" style="width:15em;" | Label
! scope="col" style="width:36em;" | Value
{{If then show|{{#invoke:WikidataIB|getValue|P26|qid=Q905|fwd=ALL|osd=no}}||<tr><th>{{#invoke:WikidataIB|getLabel|P26|qid=Q905}}</th><td>|</td></tr>}}
|}
→
Label Value
package.lua第80行Lua错误:module 'Module:i18n' not foundpackage.lua第80行Lua错误:module 'Module:i18n' not found