JBake:列出所有带有特定标签的帖子 (tagged_posts)
JBake: list all posts with a particular tag (tagged_posts)
在 freemarker 中,如何遍历所有带有特定标签的博客文章,例如标签“algorithms”?
<#assign relatedBlogs = ...>
<#if relatedBlogs?size > 0>
<h2>Related blog posts</h2>
<ul>
<#list relatedBlogs as blog>
<li>
<a href="${content.rootpath}${blog.uri}">${blog.title}</a>
</li>
</#list>
</ul>
</#if>
这对我不起作用:
<#assign relatedBlogs = tagged_posts["algorithms"]>
这也不行:
<#assign relatedBlogs = tags["algorithms"].tagged_posts>
tags
是一个序列,要使用 tags[...]
你需要一个散列。
我最终让它通过 ?filter()
和 ?first
:
工作
<#assign relatedTags = tags?filter(tag -> tag.name == "algorithms")>
<#if relatedTags?size > 0>
<#assign relatedTag = relatedTags?first>
<h2>Algorithms related blog posts</h2>
<ul>
<#list relatedTag.tagged_posts as blog>
<li>
<a href="${content.rootpath}${blog.uri}">${blog.title}</a>
</li>
</#list>
</ul>
</#if>
这不会很好地扩展,因为filter()
迭代每个页面的标签,但它对于具有数百个页面的网站来说足够快了。 Here's the related issue.
在 freemarker 中,如何遍历所有带有特定标签的博客文章,例如标签“algorithms”?
<#assign relatedBlogs = ...>
<#if relatedBlogs?size > 0>
<h2>Related blog posts</h2>
<ul>
<#list relatedBlogs as blog>
<li>
<a href="${content.rootpath}${blog.uri}">${blog.title}</a>
</li>
</#list>
</ul>
</#if>
这对我不起作用:
<#assign relatedBlogs = tagged_posts["algorithms"]>
这也不行:
<#assign relatedBlogs = tags["algorithms"].tagged_posts>
tags
是一个序列,要使用 tags[...]
你需要一个散列。
我最终让它通过 ?filter()
和 ?first
:
<#assign relatedTags = tags?filter(tag -> tag.name == "algorithms")>
<#if relatedTags?size > 0>
<#assign relatedTag = relatedTags?first>
<h2>Algorithms related blog posts</h2>
<ul>
<#list relatedTag.tagged_posts as blog>
<li>
<a href="${content.rootpath}${blog.uri}">${blog.title}</a>
</li>
</#list>
</ul>
</#if>
这不会很好地扩展,因为filter()
迭代每个页面的标签,但它对于具有数百个页面的网站来说足够快了。 Here's the related issue.