0

I am using html views. Is there a something like this? :

{{include head.html}}
index
</body>
</html>
ozer
  • 335
  • 1
  • 5
  • 13

4 Answers4

1

What you are looking for is a templating engine, since you mentioned the {{ }}tags I am assuming you are using Hogan.js aka moustache(the javascript version).

The documentation can be found here and what you are specifically looking for is the partials section.

Please note, the default express app(if you select hogan) comes installed with the hjs module which does not support partials, you will need to install the hogan-express module and replace them.

A partial looks like so:

{{> head}}
index
</body>
</html>

Partials are sent from a get or post object like so:

res.render('index.html', {
    partials: {
        head: 'partials/head.html'
    }
});
Max
  • 2,710
  • 1
  • 23
  • 34
0

You are looking for a template engine for nodejs?

For example check here: http://node-modules.com/search?q=template+engine

CFrei
  • 3,552
  • 1
  • 15
  • 29
0

I found the solution. server.js

var hbs = require('hbs');
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
hbs.registerPartials(__dirname + '/views/'); <-------- include folder

index.html Index.html includes head.html like this:

{{> head}}
index
</body>
</html>
ozer
  • 335
  • 1
  • 5
  • 13
-1

If you do not want to use ejs or jade etc then you could do it with jquery. Put this code in index.html

<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script> 
  $(function(){
     $("#header").load("header.html"); 
     $("#footer").load("footer.html"); 
});
</script> 
</head>
<body>
<div id="header"></div>
    <!--Remaining section-->
<div id="footer"></div>
</body>
</html>
Waqas Ahmed
  • 473
  • 5
  • 21