Skip to content

ferlores/handlebars-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

handlebars-server

The goal of this module is to make possible the use Handlebars in systems that cannot run javascript (Java, PHP, etc), using nodejs instead of rhino or other havier stuff :D

It exports a Connect/Express middleware and a method for using with sockets

Usage

Make a directory with your handlebar stuff:

|-templates
  |--> helpers.js
  |--> template1.html  
  |--> template2.html
  |--> partials 
    |---> partial1.html
    |---> partial2.html

Create a HTTP server with connect:

var connect = require('connect'),
    handlebars = require('handlebars-server')('./templates'),

var httpServer = connect()
  .use(connect.bodyParser())
  .use('/handlebars', handlebars.middleware)
  .listen(3000);

Now you are ready to make POST requests to ´´´http://localhost:3000/handlebars´´´. You can also use the module like this:

var handlebars = require('handlebars-server')('./templates'),
    request = {
      "template": "templateName",
      "context": {
        "msg": "hello!"
      }
    };

handlebars(request); // returns the rendered template

Enjoy!

Instalation

npm install handlebars-server

Tests

Run the tests:

  • npm install -d
  • node test/server.js
  • npm test

NOTES

  • To access to the partials just refer to them like {{> partial1}}
  • The partials directory can have subfolders. To access to those partials use ´´´{{> folder/partial2}}´´´
  • For examples look at the test directory
  • This module can also be used with file sockets:
var handlebars = requiere('handlebars-server')('./templates'),
  socketServer = net.createServer(function (client) {
    client.on('data', function(data) {
      try {
        var req = JSON.parse(data.toString());
        client.write(handlebars(req));
      }catch(e){
        console.log(e);
        client.write(e.toString() + '\r\n');
      }
    });
  })
  .listen('/tmp/handlebars.sock');

TODO

  • make automated tests for sockets

About

handlebar templating service using nodejs and sockets

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published