经过反复试验,我终于可以列出帖子/页面中的所有标签。
数据模型的 JBake 文档只记录了全局数据模型,所以下面列出的模型是供全局使用的,不是我想要的。
- 所有标签
- 标签
- tagged_posts
- tagged_documents
帖子/页面的标签实际上存在于content 数据模型中。您可以使用${content.tags} 访问它,这将列出与您想要的帖子/页面相关的所有标签。
现在,打印它是棘手的部分。因为content模型中的tags键只存在于post/page模板中。
在post / page 模板上打印标签。
当您在post / page 模板下工作时,列出所有帖子/页面标签会更容易。你需要做的是迭代content.tags。
<#list content.tags as tag>
${tag}
</#list>
在post / page 模板之外打印标签。
这种情况是我需要在html head部分生成<meta name="keywords" content="" />。当元关键字呈现在post / page 模板之外时,我需要特殊处理,例如index.html。
因为当不在post / page 模板中时,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>