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

dynamically include a partial #181

Open tzookb opened 8 years ago

tzookb commented 8 years ago

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?

jjwilly16 commented 8 years ago
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