0
votes

I have a template for articles on my site, then have each article stored as it's own HBS file. I render the pages with express. It's pretty easy to inject HTML into a handlebars template, but can I inject handlebars code (without compiling it separately first)?

here is a simplified version of how I'm getting the HTML and compiling the template:

    var articleHtml = fs.readFileSync('top10things.hbs', 'utf8');

    res.render('article', {
        title: 'article title',
        html: articleHtml,
    });

Here's what the article template might look like:

<h1>{{title}}</h1>
{{{html}}}

This works fine for html, but if my article is itself a handlebars template, it just prints out the handlebars stuff rather than rendering it.

Is there any way to have handlebars render them both together, rather than having to first render the html for the article content, then pass that html to the article template which again gets rendered?

1

1 Answers

0
votes

You should compile the article template before to inject it:

let temp = Handlebars.compile(articleHtml);
res.render('article', {
    title: 'article title',
    html: temp(context),
});

where context is your json data for the article (empty if none)