Skip to content

Commit

Permalink
publish to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Aug 16, 2012
1 parent 0586582 commit 31b3866
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
node_modules
20 changes: 12 additions & 8 deletions README.textile
Expand Up @@ -5,6 +5,10 @@ I'm tired of the lack of good templating systems for large js apps. The biggest

I hope that Nunjucks can be better. It is heavily jinja2 inspired because I think it is a great solution for templating. Nunjucks does not claim to be a direct port of jinja2, but most things will work the same in both projects.

h2. Installation

bc. npm install nunjucks

h2. Using

Here is an example template that works with the current code:
Expand Down Expand Up @@ -58,25 +62,25 @@ Hello {{ name|title }}!

p. To compile templates, you can create a Template directly or load one from an environment object. Right now the environment just loads the template from the disk relative to the current location.

bc.. var e = require('./environment');
var tmpl = new e.Template('Hello {{ username }}');
bc.. var nunjucks = require('nunjucks');
var tmpl = new nunjucks.Template('Hello {{ username }}');
console.log(tmpl.render({ username: "james" }));

// From an environment
var env = new e.Environment();
var tmpl = env.get_template('test.html');
var env = new nunjucks.Environment();
var tmpl = env.getTemplate('test.html');
console.log(tmpl.render({ username: "james" }));

h2. Express

To integrate with express, do the following (this will change before the first release):

bc.. var env = require('<src>/environment');
var loaders = require('<src>/loaders');
bc.. var nunjucks = require('nunjucks');
var loaders = nunjucks.loaders;
var express = require('express');

var e = new env.Environment(new loaders.FileSystemLoader('templates'));
app.engine('html', e.express());
var env = new nunjucks.Environment(new loaders.FileSystemLoader('templates'));
env.express(app);

h2. Status

Expand Down
14 changes: 14 additions & 0 deletions index.js
@@ -0,0 +1,14 @@

var env = require('./src/environment');
var loaders = require('./src/loaders');
var compiler = require('./src/compiler');
var parser = require('./src/parser');
var lexer = require('./src/lexer');

module.exports.Environment = env.Environment;
module.exports.Template = env.Template;

module.exports.loaders = loaders;
module.exports.compiler = compiler;
module.exports.parser = parser;
module.exports.lexer = lexer;
21 changes: 21 additions & 0 deletions package.json
@@ -0,0 +1,21 @@
{
"name": "nunjucks",
"description": "A jinja inspired templating engine",
"version": "0.1.0a3",
"author": {
"name": "James Long",
"email": "longster@gmail.com"
},
"dependencies": {
"should": "1.1.0",
"underscore": "1.3.3"
},
"engines": {
"node": "*"
},
"scripts": {
"test": "make test"
},
"main": "index"
}

20 changes: 10 additions & 10 deletions src/compiler.js
Expand Up @@ -12,15 +12,15 @@ var Frame = Object.extend({
this.variables = [];
},

add_variable: function(name) {
addVariable: function(name) {
this.variables.push(name);
},

remove_variable: function(name) {
removeVariable: function(name) {
this.variables = _.without(this.variables, name);
},

find_variable: function(name) {
findVariable: function(name) {
return _.indexOf(this.variables, name) !== -1;
}
});
Expand Down Expand Up @@ -130,7 +130,7 @@ var Compiler = Object.extend({
compileSymbol: function(node, frame) {
var name = node.value;

if(frame.find_variable(name)) {
if(frame.findVariable(name)) {
this.emit('l_' + name);
}
else {
Expand Down Expand Up @@ -176,7 +176,7 @@ var Compiler = Object.extend({
var name = node.name;
this.assertType(name, nodes.Symbol);

this.emit('env.get_filter("' + name.value + '")');
this.emit('env.getFilter("' + name.value + '")');
this._compileAggregate(node, frame, '(', ')');
},

Expand All @@ -203,15 +203,15 @@ var Compiler = Object.extend({
this._compileExpression(node.arr, frame);
this.emitLine(';');

frame.add_variable(node.name.value);
frame.addVariable(node.name.value);

this.emitLine('for(var ' + i + '=0; ' + i + ' < ' + arr + '.length; ' +
i + '++) {');
this.emitLine('var ' + v + ' = ' + arr + '[' + i + '];');
this.compile(node.body, frame);
this.emitLine('}');

frame.remove_variable(v);
frame.removeVariable(v);
},

compileBlock: function(node, frame) {
Expand All @@ -224,7 +224,7 @@ var Compiler = Object.extend({
throw new Error('cannot extend multiple times');
}

this.emit('parentTemplate = env.get_template(');
this.emit('parentTemplate = env.getTemplate(');
this._compileExpression(node.template);
this.emitLine(');');

Expand Down Expand Up @@ -271,9 +271,9 @@ var Compiler = Object.extend({
'"' + name + '", ' +
'b_' + name + ');');

frame.add_variable('super');
frame.addVariable('super');
this.compile(block.body, frame);
frame.remove_variable('super');
frame.removeVariable('super');

this.emitFuncEnd();
}, this);
Expand Down
10 changes: 5 additions & 5 deletions src/environment.js
Expand Up @@ -18,15 +18,15 @@ var Environment = Object.extend({
this.filters = builtin_filters;
},

add_filter: function(name, func) {
addFilter: function(name, func) {
this.filters[name] = func;
},

get_filter: function(name) {
getFilter: function(name) {
return this.filters[name];
},

get_template: function(name) {
getTemplate: function(name) {
var src = null;

for(var i=0; i<this.loaders.length; i++) {
Expand Down Expand Up @@ -61,7 +61,7 @@ var Environment = Object.extend({

context = _.extend(context, ctx);

var res = env.get_template(name).render(ctx);
var res = env.getTemplate(name).render(ctx);
k(null, res);
};
}
Expand Down Expand Up @@ -153,7 +153,7 @@ var Template = Object.extend({
// var env = new Environment();
// console.log(compiler.compile(fs.readFileSync('test.html', 'utf-8')));

// var tmpl = env.get_template('test.html');
// var tmpl = env.getTemplate('test.html');
// console.log("OUTPUT ---");
// console.log(tmpl.render({ username: "James" }));

Expand Down

0 comments on commit 31b3866

Please sign in to comment.