Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include HTML blocks Using node.js

This is what I want but probably can't have:

Using node.js and express and maybe ejs, I would like to, while writing a regular HTML file in my client dir, server-side-include a template block of HTML. It would be cool also if I could pass variables into the include from the HTML document.

Sooo something like:

 <!doctype html>
 <html>
   <head>
    <%include head, ({title: "Main Page"}) %>   
   </head>
   <body>
      <% include header, ({pageName: "Home", color: "red"}) %>
    ...
      <<% include footer%>>
   </body>
 </html>

Is there anyhting in node world that works like this? Or any thing that comes close and that could be maybe adapted for this functionality? I would not use it exactly in the way indicated here, but this is the functionality that I am looking for.

I have looked into jade, handlebars, ember and ejs, and ejs seems to come the closest. Maybe one of these does this already, but I am just confused about the implementation.

Any suggestions would be great!

like image 758
Jeremythuff Avatar asked May 04 '13 01:05

Jeremythuff


1 Answers

I would recommend nunjucks or pejs. Nunjucks is jinja-inspired, while pejs is just ejs + inheritance, block, and file support.

pejs has some issues with space chomping at the moment, but it's still pretty useful. Of the two, I prefer the separation layer that comes with nunjucks.

Jade is pretty cool and has the feature-set you're looking for, but it has a very unique syntax. References for jade: template inheritance, blocks, includes

like image 179
furydevoid Avatar answered Sep 21 '22 04:09

furydevoid