Giter Club home page Giter Club logo

hoganizer's Introduction

hoganizer

The Hogan

hogan.js precompiler for client side vanillaJS templates

Inspired by templatizer and built on top of hogan.js

Hoganizer?

Hoganizer precompiles mustache templates in to vanillaJS javascript functions so they render blazingly fast on the client side. This renders your app way faster because:

  • Hoganizer parses the templates way before they are shipped to the client, remember that parsing is always the most time consuming operation in template land.
  • Hoganizer outputs vanillaJS functions that only need the small template renderer from Hogan.js, this means you are not sending the whole hogan.js template engine over the wire, but only bare javascript functions and a small template wrapper.

When should I use Hoganizer?

When you are doing client side templating but want to speed up your template parsing on the frontend, Hoganizer is for you! You can use Hoganizer to compile all templates and output to a file or as a string. If you are writing mustache templates which you want to render on the backend (using express for example) this is not for you.

How do I use Hoganizer?

var Hoganizer = require('hoganizer');
var hoganizer = new Hoganizer({
    templateDir: './templates',
    extension: '.mustache',
    writeLocation: './templates.js'
});

// Compile all mustache templates in `./templates` and write
// them into frontend js file to `./templates.js`.
hoganizer.write();

// Compile but save the script as a string
var vanillaJS = hoganizer.precompile();

// Grab the latest compiled version
var vanillaJS = hoganizer.getCached();

Example

If you are working on frontend javascript website you can put all mustache templates in a templates folder and use Hoganizer to precompile the whole folder into a single vanillaJS file.

In your templates folder:

Create a file called home.mustache inside templates:

I am <em>{{name}}</em>, I like {{hobby}}!

In your frontend HTML:

<script src='templates.js'></script>

In your frontend JS

var NameOfTemplate = 'home';
var parameters = {name: 'Hulk', hobby: 'wrestling' };

var renderedTemplate = templates[NameOfTemplate].render(parameters);
// -> 'I am <em>Hulk</em>, I like Wrestling!';
$('body').html(renderedTemplate);

Production

Run hoganizer.write(); to create compiled functions and save them into templates.js, a static file which you can serve through a normal webserver.

To squeeze out the best performance, I recommend JS minifying the resulting templates.js (and for example concatenating it with the rest of your frontend JS) as well as HTML minifying the mustache files before they are written to vanillaJS by the write method (because after this process JS minifiers do not touch compiled JS strings).

Warning: don't use the precompile & write methods on a in production running nodejs webserver. They use sync fs methods and thus block your whole node server!

Dev

You can route your dev environment through an express server for example and you can serve all static files normally but generate templates on the fly for requests to templates.js. This way you can keep editting the templates in the templates folder and on every refresh you get the latest version served.

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

app.configure(function(){
    app.use(app.router);
    app.use(express.errorHandler({dumpExceptions:true, showStack:true}));
    // serve all files out of the folder `static`
    app.use(express.static(__dirname + '/static'));
});
var port = 1337;
app.listen(port);
// browse to localhost:1337

And you can catch all calls to /templates.js and serve a fresh version of your compiled templates.

var Hoganizer = require('hoganizer');
var hoganizer = new Hoganizer();
app.get("/templates.js",  function(req, res) {
    res.contentType(".js");
    res.send(hoganizer.precompile());
});

hoganizer's People

Contributors

askmike avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.