Show taxonomy content if defined

Is there an “official” way to know, if a term in a taxonomy has a defined content file? I am talking about this setup (should I use tag then I mean term of course):

  • /content/tags/term1/_index.md exists
  • /content/tags/term5/_index.md exists
  • files for term2 to term4 don’t exist
  • ranging through the terms with eg. {{ range .Site.Taxonomies.tags }}

Do we know which of the terms inside of the range are defined via content file (term1 or term5)?

I can see a way by adding frontmatter that I check, but maybe there is already an internal way do separate these two term-“types”.

Yes there is and it is quite simple actually.

Check for the existence of a parameter defined in the _index.md of said taxonomy terms.

No parameter = No _index.md

Otherwise there is no way to know if there is an _index.md unless of course you throw Section variables into your logic mix (but that is probably bound to get slightly convoluted).

IIRC, there is dicussion about this (whether Page is backed by content file or not) and the solution is by checking .File.* variables.

1 Like

That’s what I was doing. But in best case there wouldn’t be a parameter, just a title and content. I am checking for empty content now.

I’ll try my luck with that. Sounds useful.

Right now I am doing an extra round by getting the file even manually (this is for showing n=4 boxes of often used tags with descriptions if available):

{{- $counter := 0 -}}
<section id="menu">
  <div class="container">
    <div class="row">
      <div class="col-12">
        <h2>Hot {{ .Name }}</h2>
      </div>
      {{- range $key, $taxonomy := .Site.Taxonomies.tags.ByCount -}}
        {{- if lt $counter 4 -}}
          {{- with site.GetPage (printf "/tags/%s" .Name) -}}
            {{- if ne .Content "" -}}
              {{- $post := dict "context" . "fullcontent" false -}}
              <div class="col-md-6 mb-3">
                {{- partial "content/post.html" $post -}}
              </div>
              {{- $counter = add $counter 1 -}}
            {{- end -}}
          {{- end -}}
        {{- end -}}
      {{- end -}}
    </div>
  </div>
</section>

I think that your request has merit, I’ve also needed a straightforward method to detect Page Bundles in the past.

So…

1 Like

As per @jmooring 's suggestion on GitHub, you could use .BundleType to check whether a page is a Bundle.

For example you could do

{{ with .BundleType }}
<--- some markup for Taxonomy Term that have an _index.md --->
{{ else }}
<--- markup for Taxonomy Terms sans _index.md --->
{{ end }}

Also the .BundleType method returns the values: leaf , branch , or an empty string.

P.S. I closed the GitHub issue that I linked to above.

You cannot use .BundleType to determine if a term page is backed by a file. It returns branch either way.

@pamubay got it right:

{{ if .File }}
<--- some markup for Taxonomy Term that have an _index.md --->
{{ else }}
<--- markup for Taxonomy Terms sans _index.md --->
{{ end }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.