How to access Query Params on #get helper?

I have a URL like below,

/resources/?type=case-studies

I try to filter using given type value as below,

{{#get "posts" filter="primary_tag:{{@query.type}}" order="published_at desc" as |posts|}}
  {{#if posts}}
    <ul>
    {{#foreach posts}}
      <li><a href="{{url}}">{{title}}</a></li>
    {{/foreach}}
    </ul>
  {{/if}}
{{/get}}

But its not working correctly. i cannot get given query string type value.

1 Like

That’s not valid syntax for the get helper. Check out the docs for the types of filtering that are possible.

If you say more about what you’re trying to do, we can offer some possible ideas!

@RyanF I know my syntax is incorrect where i try to access the query param. You mentioned article there is no any thing about query param. I want to know, how to access query param on handlebar ?

1 Like

I don’t think the version of handlebars that Ghost uses can do that.

If you have a small number of ‘types’, you could set up a route (in routes.yaml) for each one, with the appropriate filter.

If you have a lot of types or need more complicated filtering, you could use javascript on the client side to retrieve the appropriate posts from the API and render them. (Themes with ‘load more’ buttons that append posts onto the bottom of the page are doing this, so that could be a good place to start.)

1 Like

@Cathy_Sarisky I agree with you and thanks for the response.

If i use client side java-scripts with Ghost API, It is not good for the SEO. That is the actual problem which is i am facing now.

1 Like

@RyanF, If we can’t access query param data in handlebar files as said by @Cathy_Sarisky, it is a silly and avoidable drawback of this CMS. I suggest you, implement this feature as a built-in helper.

2 Likes

You can make a case for the feature in the #ideas channel :smile:

What are you trying to accomplish, aside from using the query params? As Cathy mentioned, there are likely other methods in Ghost for accomplishing what you want, which can ultimately be preferable to a query param, even when it comes to SEO.

@RyanF i need to create a post portfolio page with a filter with pagination. Also i want share the link which is having the filter results. What are the options in ghost have to create a complex filter?

1 Like

The way to organize and present content like this in Ghost is not by filtering via query params, but rather via something like Channels and custom routes.

(the below link jumps to the Channels section)

(Collections are another, different option, also found on the page linked above).

In example:
A Channel on a route of /resources/ with some tags case-studies, other-tag-1, other-tag-2

//routes.yaml
routes:
  /resources/:
    controller: channel
    template: resources
    filter: tag:[case-studies,other-tag-1,other-tag-2]

The tag case-studies still has its index, /tag/case-studies/, and maybe there is some additional custom routing that can be done for that (not sure).

A Channel or Collection approach is more preferable to query params for any SEO concerns.

1 Like

@hwlmatt What if i need filter with multiple tags?

Example: I need to load all case-studies and datasheets.

how to manage this using your method ?

Blockquote What if i need filter with multiple tags?
Example: I need to load all case-studies and datasheets.

If filtering the sub categories of resources for display purposes is a UX concern, there are a bunch of ways to approach that depending on how you need it to work. Dynamically with javascript being one, or perhaps nested route-dependent #has blocks in the template, or by a combination of collections and channels.

routes:
  /resources/case-studies/:
    controller: channel
    filter: tag:resources+tag:case-studies
  /resources/datasheets/:
    controller: channel
    filter: tag:resources+tag:datasheets
  /resources/case-studies-and-datasheets/:
    controller: channel
    filter: tag:resources+tag:case-studies+tag:datasheets

collections:
  /:
    permalink: /{slug}/
    filter: primary_tag:-resources
    template: index
  /resources/:
    permalink: /resources/{slug}/
    filter: primary_tag:resources
    template: resources
    data: tag.resources

You could even throw an edit in the redirects file for sending /tags/resources, etc, to /resources, etc. as needed.