How to use functional helpers to select index, with custom routing?

I’m going to do my best to lay out the issue here. First of all, I have a custom home.hbs added which is displayed here.

General info:

My routes.hbs is currently:

  /:
    template: home
  /join/: members/signup
  /access/: members/signin

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

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

I’m still getting the hang of routes.hbs and how it affects things, so something here could be misconfigured.

Now the issue lies in the /culture/ page. I have my posts listed here, but I’m running into these issues:

  • Site metadata isn’t properly showing up. It’s missing a featured image.
  • I want to give it a specific <title> value in the <head>.

What I’m trying to do is find a helper like #has or #is that would allow me to:

  1. Give the /culture/ page a specific title.
  2. Add metadata for SEO.
  3. Display page counts like “(Page 3)” after the title, but not on the first page.

I know there are a few things here but I’ve looked everywhere for an answer and I think it’s just a unique combination of configurations.

Just to clarify, the routes file should be named routes.yaml :blush:

I’m not entirely sure what SEO metadata you want to change, but here’s a method of applying your custom page title and pagination number:

Replace the {{meta_title}} in the default.hbs with the following:

<title>{{{block "title"}}}</title>

This will allow you to customise the page title in all templates. So in most cases you’ll want to set it to the meta_title. That can be done by adding the following to the template:

{{#contentFor "title"}}{{meta_title}}{{/contentFor}}

Meanwhile in your culture index.hbs template we can get a bit more clever. The pagination.page helper can give us the page count, but we can also use this count to identify if it’s the first page or not with the plural helper.

Putting that all together gives us something like this:

{{#contentFor "title"}}Culture{{plural pagination.page empty='' singular='' plural=' – (Page %)'}}{{/contentFor}}

The plural helper will return nothing if it’s page 1, but will return – (Page X) if it’s page 2 and over.

Hope this is clear enough for you. If not just say :v:

1 Like

Of course it’s written in YAML :man_facepalming:t3:

Thanks, this is perfect. I think the plural helper will provide everything I need for the title.

As for the metadata I’ll have to give it a go and make sure it solves what I’m describing. I might have missed some things when I laid out the issue - it’s still a bit of a mystery what happens architecture-wise when I add the culture collection in routes.yaml but I’ll do some more research to try and figure that out.

1 Like

Hey @explore, just thought I’d let you know we’ve updated our docs to include the block and contentFor helpers which were the helpers I suggested for your site title issue. Hope this helps in future:

That’s great! The docs are getting better and better :clap:t2:

1 Like

David, old thread, but I’m struggling with this. I have a collection set up in routes.yaml for mysite.com/newsletter, but I can’t figure out to get it to insert “Newsletter” as the page name. I tried what you suggested above, but it has no impact on page name. I modified default.hbs, and added to my template archive.hbs.

Ideas?

If you’ll post excerpts of the relevant parts of these files, it’ll help us help you!

Hi Cathy. I actually figured out today how to do it: Turns out I didn’t understand what data: did in routes.yaml, and now realize that I can use it to feed the metadata/title/etc. from a tag that is behind a collection to the title of that collection.

2 Likes