1

I created a basic Node.js webserver with this code:

'use strict';

var express = require('express');
var app = express();

var server = app.listen(1337, '127.0.0.1', function () {

  //app.use(express.static(__dirname + '/public'));
  app.use(express.static(__dirname + '/'));


  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

I have a basic HTML5 Boilerplate. What I want in the future is to have a separated HTML file for navigation, which will only contain this:

<nav>
  <ul>
    <li>menu item</li>
    <li>menu item</li>
    <li>menu item</li>
  </ul>
</nav>

And this HTML file, for example 'nav.html" will embedded in the right place of my document. I done this in the past with PHP's require_once (if I remember well). I want to separate my site's header, content, footer, navigation in different document's, because if I changing the menu (adding, deleting pages), I don't need to re-edit every single page. So I want something similar to do a static site with Node.js. I'm installed Express and Handlebars.

Is it possible somehow?

I hate Jade, that's why I don't want to use it. I just don't like when I don't see my final code in the editor.

Lanti
  • 2,299
  • 2
  • 36
  • 69
  • "I just don't like when I don't see my final code in the editor." Really? That's exactly what you're asking for... a template engine that can include files to make a composite output made of individual pieces, none of which represent that final code as you see it in your browser. And, you don't have to use Jade, you just mentioned you want to use Handlebars... – Brad Nov 28 '14 at 21:30
  • Okay, correction: "Barely" the final code... So, is it possible somehow? Probably you have your preferences how you like to edit your code. I just simply don't like how Jade simplifies to a "skeleton in a desert" the code. But I DO like when I can do this with Stylus in CSS. – Lanti Nov 28 '14 at 21:36
  • Ah, that is much clearer. I thought you were saying that you didn't want to use any template, but needed a template engine. Yes, I'm not a fan of Jade either. Anyway, you probably want a partial: http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers – Brad Nov 28 '14 at 21:42
  • Thank You the info! I think this is what I need. I hope it's working with classes too: http://stackoverflow.com/questions/11523331/passing-variables-through-handlebars-partial – Lanti Nov 28 '14 at 21:53
  • Handlebars is client or server side? Maybe it's an alternative solution to write HTML syntax in JADE and use only JADE syntax for embedding other parts of the document? For example separate header, content, footer, navigation? – Lanti Nov 29 '14 at 00:48
  • Nope, Jade not works this way, unfortunately... – Lanti Nov 29 '14 at 00:48
  • Use handlebars or Jade, not both. Both can work client and server-side. It's JavaScript, so a lot of the libraries cross over. – Brad Nov 29 '14 at 00:49
  • I just found Total.js, which is little code size when installed, fast, totally independent (well, I really don't care but I like how small it is) and I can do the basic routing for my static website that I really wanted. Express.js core distributor left from project and went with Go... – Lanti Nov 29 '14 at 21:11
  • Express can do your routing... you said you were looking for a template engine? In any case, I'm glad you found something that works for you. – Brad Nov 29 '14 at 21:12

0 Answers0