Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

publish to npm

  • Loading branch information...
commit 31b38660996df23bcd3e5d114bd9c75f7d5f529a 1 parent 0586582
@jlongster authored
View
1  .npmignore
@@ -0,0 +1 @@
+node_modules
View
20 README.textile
@@ -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:
@@ -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
View
14 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;
View
21 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"
+}
+
View
20 src/compiler.js
@@ -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;
}
});
@@ -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 {
@@ -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, '(', ')');
},
@@ -203,7 +203,7 @@ 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 + '++) {');
@@ -211,7 +211,7 @@ var Compiler = Object.extend({
this.compile(node.body, frame);
this.emitLine('}');
- frame.remove_variable(v);
+ frame.removeVariable(v);
},
compileBlock: function(node, frame) {
@@ -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(');');
@@ -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);
View
10 src/environment.js
@@ -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++) {
@@ -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);
};
}
@@ -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" }));
Please sign in to comment.
Something went wrong with that request. Please try again.