Use the {{lang}} helper in filters


#1

Hi!

I’m working on a blog which will contain multi-language articles (some in French, others in Italian or German). They will be tagged with an internal tag like #fr, #it, #de, etc.

On the front page of the blog, I’d like to retrieve and display only posts that have a the tag that matches the blog locale (set in the admin panel) ; I intend to have three instances of Ghost on my server, each of them having the locale set to one of the three languages.

For example, if I set the blog locale to fr, I want the posts that have the #fr tag.

I tried the following to do that (note the first line):

{{#get 'posts' limit=12 include="tags,authors" filter="tag:hash-{{lang}}" as |articles|}}
  <section id="front-page-teasers">
    {{#foreach articles}}
      <div class="front-page-teaser">
        {{> "article-mini" isFrontPage=true}}
      </div>
    {{/foreach}}
  </section>
{{/get}}

And… this doesn’t work. Sadly.
I understand, thanks to the syntax highlighting here on the forum, that it’s because the {{lang}} in the filter string seems to be interpreted as a string, not as a handlebar helper. Thus, the filter tries to retrieve posts with the #{{lang}} tag.

Is there any way to do what I want to achieve? What would be a workaround?

Thanks !


#2

Interesting use-case! Basically the use-case for this is just to be able to use the same theme on 3 different sites, right?

I think this should be possible, cause do some similar tricks in https://github.com/TryGhost/Casper/blob/master/post.hbs to get dynamic data outside of the current context

Just eyeballing it, I’d say start with trying {{../lang}} or {{../../lang}} to see if that lets you get there. Just a guess though, I haven’t tried this specific case before :slight_smile:


#3

The {{lang}} helper is actually deprecated in favour of {{@site.lang}} - I’ve made a note to get that updated in the docs asap.

However, this won’t help you because the get helper throws an error if you pass in {{@site.lang}}. This is a bug, which I have raised here:

The workaround here would be to use the dynamic routing feature, instead of the get helper.

There’s a specific tutorial here: