Search code examples
javascriptnode.jssvghandlebars.jsexpress-4

Passing an include file into a Handlebars layout


I'm using Node.js, Express 4, and the Handlebars templating processor. Some of the page views that I'm rendering with Handlebars have several kBytes of static inline SVG code. Is there a simple clean way to put the SVG code into a separate file to be included in the Handlebars layout template? Ideally this include file would have a .svg extension, but .hbs would be acceptable.


Solution

  • yesterday I was solving the the same problem. And I finished with loading svg file into string and then pass it to the handlebars template.

    var svgTemplate = fs.readFileSync('./public/app/build/images/spriteAll.svg', 'utf8');
    
    var express = require('express'),
        router = express.Router();
    
    router.get('/',  function (req, res) {
        res.render('main', {
            svgTemplate: svgTemplate
        });
    });
    

    //where main.hbs contains: ...

    <body class="ng-cloak">
        <div class="inline-svg">
            {{{svgTemplate}}}
        </div>
    ....
    </body>