0

I have an urgent demo showcase for Ghost and I have the following context. Though not specific to Ghost, its a general problem

{
  posts: [{
    name: 'post1',
    id: 1,
    tags: [{
      id: 1,
      name: 'tag1'
    },{
      id: 5,
      name: 'tag5'
    }]
  },
  {
    name: 'post2',
    id: 2,
    tags: [{
      id: 1,
      name: 'tag1'
    },{
      id: 3,
      name: 'tag3'
    }]
  }]
}

I want to create a filter helper in HandleBars.js which will display all posts whose tag name is 'tag1'

Something like

{{#filter posts tags[].name="tag5"}}
   <h1> {{name}} </h1>
{{/filter}}

Outputs

<h1> post1 </h1>

EDIT I have seen filter helpers, I think what I need is deepfilter helper.

Gaurav Ramanan
  • 3,655
  • 2
  • 21
  • 29
  • You should pre-filter in the code that populates the template - this kind of logic is really too complex for handlebars. – joews Mar 22 '15 at 08:16
  • Thanks for your comment @joews. In this specific case I'm using Ghost CMS where the *theme* decides what to show, how to show it and where. The logic of filtering cannot escape the theme into the core. – Gaurav Ramanan Mar 22 '15 at 08:31
  • So, what's a problem? Use Array#filter (or other filter you like) with any implementation of path resolver for JS objects, e.g. this one https://stackoverflow.com/a/6491621/1913708 – raidendev Mar 23 '15 at 05:34

0 Answers0