6

I'm trying to use dynamic partials in Handlebars, using Gulp and gulp-compile-handlebars.

I've seen this roll-your-own solution but I believe that Handlebars now supports dynamic partials out of the box, however I don't understand what the documentation is expecting from me.

In my hbs template I want to specify the name of the partial based on a value in my data, e.g.

"things": [
  {
    "partial_template": "thing1",
    "title": "Thing 1"
  },
  {
    "partial_template": "thing2",
    "title": "Thing 2"
  }
]

With the partials named as:

thing1.hbs, thing2.hbs, etc.

I don't think my requirements need to use subexpressions so was expecting to use the lookup helper as per the Handlebars docs:

{{> (lookup . 'partial_template') }}

But this errors when running Gulp.

Any suggestions as to where I'm going wrong please?

Community
  • 1
  • 1
Frank Furter
  • 508
  • 1
  • 6
  • 15
  • 1
    Hello, why not build a custom helper that holds a dictionary for all the possible partials (precompiled) and that returns the appropriate content? Would that be an option? – Mathieu de Lorimier Mar 16 '18 at 12:46

0 Answers0