Sukma Wardana
Sukma Wardana

Reputation: 550

How to list all tags from post / pages

I would like to print tags that related with post / page with Jbake. But, after read the JBake documentation, especially for Data Model documentation so far I'm aware about:

But, among those list there is no data model for tags that specific to post / page. It's possible to print tags specific to post / page?

I'm using:

Upvotes: 0

Views: 132

Answers (1)

Sukma Wardana
Sukma Wardana

Reputation: 550

After do trial and error finally I could list all tags from post / page.

JBake documentation for data model is only documented for global data model, so listed below model is for global use and is not what I want.

  • alltags
  • tags
  • tagged_posts
  • tagged_documents

The tags for post / page is actually exist within content data model. You can access it with ${content.tags}, this will list all tags that related with post / page that you want.

Now, to print it is the tricky part. Because the tags key within content model is only exist within post / page template.

Print tags on post / page template.

List all post / page tags is easier when you're working under post / page template. What you need to do is iterate the content.tags.

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

Print tags outside post / page template.

This situation is when I need to generate <meta name="keywords" content="" /> on the html head section. I need special handling when the meta keywords rendered outside post / page template, for example index.html.

Because when not in post / page template the content data model only contains two keys: rootpath and type. So, need special handling like below:

<#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>

Upvotes: 2

Related Questions