我想用 Jbake 打印与帖子/页面相关的标签。但是,在阅读了 JBake 文档之后,尤其是到目前为止我知道的数据模型文档:
- 所有标签
- 标签
- 标记的帖子
- 标记文档
但是,在这些列表中,没有特定于帖子/页面的标签的数据模型。可以打印特定于帖子/页面的标签吗?
我在用着:
- JBake 版本 2.7.0-rc.4
- Apache Freemarker 作为模板引擎
我想用 Jbake 打印与帖子/页面相关的标签。但是,在阅读了 JBake 文档之后,尤其是到目前为止我知道的数据模型文档:
但是,在这些列表中,没有特定于帖子/页面的标签的数据模型。可以打印特定于帖子/页面的标签吗?
我在用着:
经过反复试验,我终于可以列出帖子/页面中的所有标签。
数据模型的 JBake 文档只记录了全局数据模型,所以下面列出的模型是供全局使用的,不是我想要的。
帖子/页面的标签实际上存在于content
数据模型中。您可以使用 访问它${content.tags}
,这将列出与您想要的帖子/页面相关的所有标签。
现在,打印它是棘手的部分。因为模型中的tags
键content
只存在于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
/ page
template 中时,content
数据模型只包含两个键:rootpath
和type
. 因此,需要特殊处理,如下所示:
<#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>