0

我想用 Jbake 打印与帖子/页面相关的标签。但是,在阅读了 JBake 文档之后,尤其是到目前为止我知道的数据模型文档:

  • 所有标签
  • 标签
  • 标记的帖子
  • 标记文档

但是,在这些列表中,没有特定于帖子/页面的标签的数据模型。可以打印特定于帖子/页面的标签吗?

我在用着:

  • JBake 版本 2.7.0-rc.4
  • Apache Freemarker 作为模板引擎
4

1 回答 1

0

经过反复试验,我终于可以列出帖子/页面中的所有标签。

数据模型的 JBake 文档只记录了全局数据模型,所以下面列出的模型是供全局使用的,不是我想要的。

  • 所有标签
  • 标签
  • 标记的帖子
  • 标记文档

帖子/页面的标签实际上存在于content数据模型中。您可以使用 访问它${content.tags},这将列出与您想要的帖子/页面相关的所有标签。

现在,打印它是棘手的部分。因为模型中的tagscontent只存在于post/page模板中。

post在/page模板上打印标签。

当您在post/page模板下工作时,列出所有帖子/页面标签会更容易。您需要做的是迭代content.tags.

<#list content.tags as tag>
  ${tag}
</#list>

post在外部/page模板打印标签。

这种情况是我需要<meta name="keywords" content="" />在html head部分上生成的。当元关键字呈现在post/page模板之外时,我需要特殊处理,例如index.html.

因为当不在post/ pagetemplate 中时,content数据模型只包含两个键:rootpathtype. 因此,需要特殊处理,如下所示:

<#if (content.tags)?? >
  <#-- Make sure we have tags model inside content -->
  <#list content.tags as tag>
    ${tag}<#sep>, </#sep>
  <#else>
    <#-- in case that your page don't have any tags define, print default value if possible -->
  </#list>
<#else>
  <#-- There is no tags model inside content, print default value if possible -->
</#if>
于 2021-10-06T08:15:42.690 回答