【问题标题】:How to transclude <noinclude> tags in templates to set categories only on original page如何在模板中嵌入 <noinclude> 标签以仅在原始页面上设置类别
【发布时间】:2021-01-03 18:19:26
【问题描述】:

作为模板的一部分,我想设置页面的类别。而且在嵌入该页面时也不包括该类别。

如果我有一个 Template:Person 的模板定义

<includeonly>Name: {{{1|}}}
[[Category:People]]
</includeonly>

然后在“John Smith”的人员页面中,根据需要将 John Smith 添加到人员类别中

{{Person|John Smith}}

然后我有一个页面包含 John Smith 页面,但我不希望它在人员类别中。所以说“本周员工”页面有

== Best employee ever ==
{{:John Smith}}

现在我觉得可以使用 {{subst:}}{{safesubst:}} 来完成,如下所述:https://www.mediawiki.org/wiki/Help:Substitution 但我无法弄清楚

现在我看到 wikipedia 有一个模板,它看起来像我想要的那样,https://en.wikipedia.org/wiki/Template:Noinclude 但我无法弄清楚它对我不起作用。我尝试的其中一件事是将noincludedtag 模板添加到我的wiki,然后在Template:Person 中尝试此操作,但没有成功

<includeonly>Name: {{{1|}}}
{{subst:noinclude|[[Category:People]]}}
</includeonly>

【问题讨论】:

  • 如果没有{{subst:Person|John Smith}},我认为您尝试做的事情是不可能的,它会在个人页面中扩展模板,模板可能会在类别周围添加&lt;noinclude&gt; 标签。

标签: mediawiki mediawiki-templates


【解决方案1】:

试试这个:

<includeonly>Name: {{{1|{{PAGENAME}}}}}
{{#ifeq:{{PAGENAME}}|{{{1|{{PAGENAME}}}}}|[[Category:People]]}}</includeonly>

解析器函数{{#ifeq:}} 由与MediaWiki 捆绑的扩展ParserFunctions 提供。它比较前两个参数,如果相等则返回第三个,否则返回第四个。

假设页面名称将与模板的参数相同;或由参数定义,如果实际模板比示例更复杂。

【讨论】:

  • 好主意,不过我不会使用默认值。这意味着在“John Doe”上写{{Person}} 是可行的,但如果您随后在其他地方包含{{:John Doe}},它将呈现“Name: Employee of the Week”(并将其包含在类别中)。
  • 我喜欢这个主意,我明天试试
  • @Bergi,很公平。使用默认值是我的习惯,而不是嵌入非模板页面。
【解决方案2】:

我选择了一个对我有用的更复杂的解决方案。我首先安装了变量扩展,https://www.mediawiki.org/wiki/Extension:Variables

然后创建了一个模板Template:SetCategories

<includeonly>{{#varexists:hidecats||{{#arraymap:{{{names|}}}|,|x|[[Category:x]]|}}}}</includeonly>

然后我(已经有)一个嵌入模板Template:IncludePageWithHeader,所以我在嵌入页面时添加了一个变量{{#vardefine:hidecats|yes}}

<includeonly>{{#switch: {{{heading_level|}}}
| 2 = == [[{{{name|}}}{{#if: {{{extra|}}}|,&#32;{{{extra|}}}|}}|{{{name|}}}]] ==
| 3 = === [[{{{name|}}}{{#if: {{{extra|}}}|,&#32;{{{extra|}}}|}}|{{{name|}}}]] ===
| 4 = ==== [[{{{name|}}}{{#if: {{{extra|}}}|,&#32;{{{extra|}}}|}}|{{{name|}}}]] ====
| === [[{{{name|}}}{{#if: {{{extra|}}}|,&#32;{{{extra|}}}|}}|{{{name|}}}]] ==
}}
{{#vardefine:hidecats|yes}}
{{:{{{name|}}}{{#if: {{{extra|}}}|,&#32;{{{extra|}}}|}}}}</includeonly>

所以现在Template:Person 变成了

<includeonly>Name: {{{1|}}}
{{SetCategories|names=People}}
</includeonly>

然后成为本周最佳员工

{{IncludePageWithHeader|name=John Smith}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-20
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多