最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing an include file into a Handlebars layout - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question asked Feb 11, 2017 at 7:50 natesrcnatesrc 611 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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>

You can make a static assets folder (e.g. /public) in your project, and include it with Express: app.use(express.static('public')); Put your .svg file in there.

Then in your handlebars file simply add the SVG as you would in a normal HTML project, like <img src="your.svg">

You could try out handlebars-partial-file to include a file's contents as a Handlebars partial.

发布评论

评论列表(0)

  1. 暂无评论