title: Using mustache.js partials with express
body: <p>So this may be a case of me completely misunderstanding functionality, but I am trying to use partials in node.js so that I have a reusable, reinsertable header and footer on my various templates similar to <code>{% extends 'something.html' %}</code> in django or <code>&lt;? includes 'something.php ?&gt;</code> in php. As I understand it this is what partials are for.</p>
  
  <p>So in my app.js uses this configuration to render templates:</p>
  
  <pre><code>var mustache = require('mustache');
  var template = {
      compile: function (source, options) {
          if (typeof source == 'string') {
              return function(options) {
                  options.locals = options.locals || {};
                  options.partials = options.partials || {};
                  if (options.body) // for express.js &gt; v1.0
                      locals.body = options.body;
                  return mustache.to_html(
                      source, options.locals, options.partials);
              };
          } 
          else {
              return source;
          }
      },
      render: function (template, options) {
          template = this.compile(template, options);
          return template(options);
      }
  };
  
  // Configuration
  app.configure(function(){
      app.register(".html", template);
      app.set('views', __dirname + '/views');
      app.set('view options', {layout: false});
      app.use(express.bodyParser());
      app.use(express.methodOverride());
      app.use(app.router);
      app.use(express.static(__dirname + '/public'));
  });
  </code></pre>
  
  <p>and then I have this route:</p>
  
  <pre><code>var header = require("../views/header.html");
  
  module.exports = function(app){
  app.all('/test', function(req, res){
      var data = {
          locals: {value: "some value"},
          partials: {header: header}
      }
      res.render('test.html', data);
  });
  </code></pre>
  
  <p>header.html is simply this:</p>
  
  <pre><code>hello world
  </code></pre>
  
  <p>and test.html is simply this:</p>
  
  <pre><code>{{&gt;header}}
  {{ value }}
  </code></pre>
  
  <p>I would expect that this would render:</p>
  
  <pre><code>hello world
  some value
  </code></pre>
  
  <p>but I get an unexpected token error when I run <code>node app.js</code> pointing to <code>hello world</code> in my header.html as the problem.  </p>
  
  <p>what am I missing in configuring this so that it will work? </p>