express-handlebars icon indicating copy to clipboard operation
express-handlebars copied to clipboard

dynamically include a partial

Open tzookb opened this issue 8 years ago • 1 comments

I want to include a partial based on a variable I pass to the view

example:

router.get('/page', function(req, res, next) {
    res.render('pages/page', {partial: 'whateverPartial'});
});

now in the view:

<h3>dont care</h3>
{{> partial}}

I browsed all over the web and found this solution, but it doesn't work. it said for placing variable instead of a string do like this

    {{> (partial) }}

but it gives me this error:

  Message: ((helpers.partial || (depth0 && depth0.partial)) || alias2).call is not a function

any ideas how to solve that?

tzookb avatar Apr 21 '16 07:04 tzookb

router.get('/page', function(req, res, next) {
    res.render('pages/page', {
        partial: function () {
            return 'whateverPartial'
        }
    });
});

When using dynamic partials, '(partial)' must be a function. See here: http://handlebarsjs.com/partials.html#dynamic-partials

jjwilly16 avatar May 16 '16 20:05 jjwilly16