1

I would like to start by saying I am new to Node.js.


My question is this. Is there a way in which a single layout can be used for the HTML? Something similar to shared views and Master Layouts in MCV and ASP respectively.

For example:

//. hello_world.hjs

<!DOCTYPE html>
<html>
  <head>
    <title>{{ title }}</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1>{{ title }}</h1>
    <div id="content">
        <!-- Place changing page content here -->
        {{> part }}
    </div>
    <footer>
        {{date}}
    </footer>
  </body>
</html>

//. hello_world.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('hello_world', { 
    title: 'Hello World!', 
    partials: { 
        part: 'part'
    }
  });
});

//. part.html

<h1>This is a part</h1>

I am only looking to change the content in the div with the id "content" in hello_world.hjs This way when an update is made to the footer or navigation bar changes won't have to be made to every document.


folder structure ->

routes
-- hello_world.js

views
--hello_world.hjs
--part.html
--index.hjs

I have tried searching for a solution. However, I am not sure if I am using the proper terminology in my searches.

K.J.
  • 921
  • 7
  • 7
  • Well first off, this doesn't actually have anything to do with Node.js or Express, which is why you're having trouble finding something. What is your template engine? Jade? Mustache? That's what you should be looking for. Node.js is an application framework. Express is a framework for building web apps with HTTP. Your template engine is what actually handles the views. – Brad May 02 '15 at 20:05
  • I am using Hogan template engine at the moment. Should I be using a different template engine? – K.J. May 02 '15 at 20:11
  • Nope, you can use whatever template engine you'd like! I'm just saying that when Googling, you should be looking for Hogan then rather than Node.js. – Brad May 02 '15 at 20:11
  • Thanks. I will post back here if I find the answer. – K.J. May 02 '15 at 20:13
  • I came across this post: https://stackoverflow.com/questions/12783615/partials-with-node-js-express-hogan-js and have tried some of the suggestions there with no luck. I get a type error. TypeError: path must be a string – K.J. May 02 '15 at 20:53

1 Answers1

1

The part.html had to be renamed to part.hjs. However, this still does not answer the entire question. Though I am now able to use partials I need to be able to use a consistent layout and I would like to not have to repeat myself with in the JS files such as:

partials: { 
    part: 'part',
    footer: 'footer'
}

Retyping that across a large website is going to get old and become prone to typos.

K.J.
  • 921
  • 7
  • 7
  • So is there a clear way to do this without referencing: https://stackoverflow.com/questions/19004391/hogan-js-with-master-pages-or-layouts?rq=1 – K.J. May 02 '15 at 22:02
  • why would you be typing that over and over if you are dynamically generating your pages with templating? I dont think you understand that point of using Hogan or Node.js for that matter. This isnt some static page with a bit of PHP thrown in. – GifCo Mar 07 '17 at 21:58