Context for Channels

In my current theme I am checking for different contexts to decide which information I display in the head portion of the blog:

[...]
<header class="bg-dark d-flex flex-column flex-grow-0 flex-shrink-0 mb-5" style="height: 45vh; min-height: 270px; background: linear-gradient(rgba(0,0,0,0.6),rgba(0,0,0,0.6)),url({{background}}); background-position: 50% 50%; background-repeat: no-repeat; background-size: cover;">
    {{> "trans-nav" }}
    <div class="container d-flex flex-column justify-content-center align-items-center h-100" style="margin-top: -56px;">
    {{#is 'index'}}
        {{#if @site.logo}}<img src="{{@site.logo}}">{{/if}}
        <h1 class="text-white font-weight-bold ">{{@site.title}}</h1>
        <p class="lead text-white-50 font-weight-bold">{{@site.description}}</p>
    {{/is}}

    {{#is 'page'}}
        <h1 class="text-white font-weight-bold display-4 text-center text-xl-left">{{title}}</h1>
    {{/is}}

    {{#is 'post'}}
        <h4 class="post-date text-white-50 font-weight-bold" >{{date published_at format="MMMM DD, YYYY"}}</h4>
        <h1 class="text-white font-weight-bold display-4 text-center text-xl-left">{{title}}</h1>iv>
    {{/is}}

    {{#is 'tag'}}
        <h1 class="text-white font-weight-bold text-capitalize">{{name}} <span class="badge badge-primary">{{total_posts}}</span></h1>
        {{#if description}}<p class="lead text-white-50 font-weight-bold">{{description}}</p>{{/if}}
    {{/is}}    
    </div>

{{else}}
[...]

I am using internal tags like #micro and #link to implement a basic version of custom post types. To be able to target only these types of posts, I decided to create custom routes/channels:

routes:
  /links/:
    controller: channel
    filter: tag:link
  /micros/:
    controller: channel
    filter: tag:micro
  /posts/:
    controller: channel
    filter: tag:-[link,micro]

collections:
  /:
    permalink: /{slug}/
    template:
      - index

taxonomies:
  tag: /tag/{slug}/
  author: /author/{slug}/

But whenever I go to the URL of one of the routes, my header is empty, as it does not fit into any of the contexts. Is there a context I can target for each of the channels/routes? Or do I have to implement some kind of default? Is there an {{#elseis}} statement for {{#is}}?

I’m not sure how you would do a default for this :thinking:

You can use an {{else}} within an {{#is}} - but that’s gonna get nasty nested.

Would you consider using a different template with the header “hard-coded”? And specifying the template in each of your routes? (Similar to how it’s defined for your collection)

You could use the {{!< layout}} functionality from express-hbs or some partials, to get rid of some duplication between the different templates - which might be a lil cleaner.

I would not want to use a different header, a more hard-coded one. Having a template per route is also not feasible IMHO, as I would need to be able to assign a template to an internal tag, which is not possible, as far as I know.

You could use the {{!< layout}} functionality from express-hbs or some partials, to get rid of some duplication between the different templates - which might be a lil cleaner.

I am not sure what you mean, can you elaborate on that please? Sounds interesting.

Having a template per route is also not feasible IMHO, as I would need to be able to assign a template to an internal tag, which is not possible, as far as I know.

I don’t think you need to assign a template to an internal tag, just wire it up in the routes.yaml

routes:
  /links/:
    template:
      - template_with_default_header
    controller: channel
    filter: tag:link
  /micros/:
    template:
      - template_with_default_header
    controller: channel
    filter: tag:micro

When I mentioned layouts I was referring to deduplicating the work to create template_with_default_header.hbs by using “layouts” or “contentFor” block from express-hbs

Thanks for the clarification, much appreciated! I totally forgot about “layout” or “contentFor”. I will see what I can do with these.

I also think I understand your point now with the template. I had a brain fart for a second, it is just the index site I need to change for each “kind”, not the site of the post itself.

Thanks! You gave me great pointers into new directions to explore :slight_smile: :+1:

1 Like

All good! I hope you get it working how you want :relaxed: