Pass variables to template via routes

I have a scenario I am trying to solve with templates and routes but I am missing basic understanding and I can’t understand the documentation at Ghost Themes - Dynamic URLs & Routing

I have created a custom page (template) called news. This page will show a static introduction and then 2 links, Media and Meetings.

I then want to template page to show all posts that have the tag news as default and then filter posts on the tags news and media or news and meetings if they click on the buttons.

My approach has been to create these routes:

    permalink: /news/
    template: news

    permalink: /news/media/
    template: news

    permalink: /news/meetings/
    template: news

news.hbs looks something like this:

<some html>
<a href="/news/">Show all</a>
<a href="/news/media/">Media</a>
<a href="/news/meetings/">Meetings</a>
{{#get "posts" limit="all" filter="a variable"}}
            <div class="gh-topic gh-topic-grid">
                <div class="gh-topic-content">
                    {{#foreach posts}}
                        {{> "loop-grid"}}

I now want to pass the filter into “a variable” from the routes so they get the values
tags:news or tags:news+tags:media or tags:new+tags:meetings

Is this possible and how can I do this?

Thank you

I have just tried different ways of filtering, and I don’t think this will work with filters alone.

The issue I see is that you can’t pass the filters from the routes.yaml into a variable that’s usable in the #get query.

I think the easiest way would be separate page templates. E.g. a news.hbs, a news-media.hbs, and a news-meetings.hbs. By using partials, you can then make sure that the static content is the same, yet you can modify the #get query as needed.

That’s unfortunate that it can’t be handled in a better way.
I ended up using your sugestion.

Thank you.

1 Like