ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 384 forks source link

How to dynamically include partials #63

Closed Raidok closed 10 years ago

Raidok commented 10 years ago

I have a JSON markup something along the lines like this:

markup/frontpage.json:

{
  title: 'Front page',
  children: [
    {
      partial: 'list',
      children: [
        { partial: 'listelement', content: 'Element 1' },
        { partial: 'listelement', content: 'Element 1' }
      ]
    }
  ]
}

And handlebars templates:

views/frontpage.handlebars:

<h1>{{title}}
{{{partial children}}}

views/partials/list.handlebars:

<div class="list-group">
  {{{partial children}}}
</div>

views/partials/listelement.handlebars:

<a href="#" class="list-group-item">{{content}}</a>

I would like to generate HTML based on similar markups but express3-handlebars expects the partial to be a name of a partial in {{> partial}} but I would like it to dynamically included by the value of this variable in the scope. It looks like a job for a helper but I can't seem to figure it out.

Has anybody else figured this one out already?

maxattensa commented 10 years ago

I am trying to achieve this too. If this is not currently possible I vote for its addition as well.

ericf commented 10 years ago

Dynamic partials are a limitation in Handlebars. So they way you'd do something like this is by adding a custom helper.

elover commented 9 years ago

I hava the qusition too. Has anybody solve the problem?

ericf commented 9 years ago

Dynamic partials are now supported in Handlebars 3.0, and PR #105 adds support for Handlebars 3.0.