Reputation: 200
I hope someone can give me a hint. I would like to import content from one file into my handlebar file. Is it possible? In my case, it is an css/scss file (e.g. reset.css) which stylings I want to import into my handlebar file (styleReset.hbs).
The "styleReset.hbs" should looks kind of like this:
<style type="text/css">
<!-- import of reset.css content -->
</style>
P.S. I don't want use the -tag
Upvotes: 6
Views: 28554
Reputation: 2943
It's working after adding it
// app.js file
app.use(express.static(path.join(__dirname, "public")));
public/
├── style.css/
// htm file
<link rel="stylesheet" href="/style.css">
Upvotes: 0
Reputation: 233
1)In order to use your .css file in handlebars, file should be registered for use in app.js/server.js file shown bellow.
app.use("/bootstrap",express.static(__dirname+"/node_modules/bootstrap/dist"))
2)import the file in your handlebar file as given bellow image(it works for both main layout and child layout.
i) In Main Layout File
ii) In Child Layout
Upvotes: 0
Reputation: 1
It is also possible to have a 'main' layout too, that can include header and footer.
app.engine('.hbs', exphbs({
extname: '.hbs',
defaultLayout: 'main'
}))
app.set('view engine', '.hbs')
Also, if you are using the module, "express-handlebars"(not "hbs"). You can set your extension name too.
Upvotes: 0
Reputation: 1
index.js
public:
style.css
views:
index.hbs
Inside index.js
var express=require('express');
var app=express();
var hbs = require('hbs');
app.set('view engine', 'hbs');
app.use(express.static('.'));
Inside index.hbs
<head>
<link href="./public/style.css" rel="stylesheet">
</head>
Upvotes: -1
Reputation: 4926
Yes, it is possible to import the external css file into your handlebars .hbs file (i.e- Template engine).
Follow these steps :
app.use(express.static(__dirname + '/public'));
<link rel="stylesheet" href="../css/style.css">
Upvotes: 13
Reputation: 5292
You can't import files with handlebars, only partials. You could precompile your reset.css as if it was a handlebars partial and include that with {{> filename}}
.
Without knowing your build setup I don't think I can go into more detail.
http://handlebarsjs.com/precompilation.html
(Personally I'd use sass to import my reset.css to some main stylesheet that I include in the page.)
Upvotes: 1