Answer the question
In order to leave comments, you need to log in
How to use source in handlebars from external file and not from inline code (to add css, js and change title)?
Very strange that this is not included in the documentation. At least I couldn't find it.
There is a standard pattern for using handlebars :
var source = "<p>Hello, my name is {{name}}. I've uploaded {{number}} of photo(s)</p>" +
"<div>{{#each element}}" +
"<p><img src='{{ this }}'></p>" +
"{{/each}}</div>";
var template = handlebars.compile(source);
var html = template(data);
console.log(html);
var source = require('./template.html')
var template = handlebars.compile(source);
var html = template(data);
console.log(html);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{title}}</title>
<link rel="stylesheet" media="all" href="/somefile.css">
<script type="text/javascript" src="/somefile.js"></script>
</head>
<body>
<h1 class="h1-like">{{sometext}}</h1>
<p>{{somepara}}</p>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
You can do this:
var fs = require('fs');
var source = fs.readFileSync('./template.html', 'utf8');
var template = handlebars.compile(source);
var html = template(data);
console.log(html);
var fs = require('fs');
fs.readFile('./template.html', 'utf8', function(err, source) {
var template = handlebars.compile(source);
var html = template(data);
console.log(html);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question