Permalink
Browse files

Import everything again.

  • Loading branch information...
0 parents commit ac1944008cab2fc41a2de8a756d29ce1d157ef1d Thomas Lee committed May 13, 2010
340 LICENSE

Large diffs are not rendered by default.

Oops, something went wrong.
101 README.md
@@ -0,0 +1,101 @@
+Jazz is a simple template engine built specifically for nodejs.
+
+# Usage
+
+ var jazz = require("jazz");
+
+ var template = jazz.compile("my template source code {someVariable}");
+ var output = template.eval({"someVariable", "lolmuffin"});
+ sys.puts(output); // or write response to server, write to a file, ...
+
+This example would output the following:
+
+ my template source code lolmuffin
+
+# Syntax
+
+!!! SYNTAX IS VERY MUCH STILL SUBJECT TO CHANGE !!!
+
+## Expressions
+
+## Printing variables
+
+ {someVariable}
+
+## Filtering values prior to output
+
+*NOT IMPLEMENTED YET*
+
+ {someVariable|someFilter}
+
+## Conditional Statements
+
+You can check if a variable evaluates to a true value like so:
+
+ {if name}
+ Hello, {name}
+ {end}
+
+Else clauses are also supported:
+
+ {if name}
+ Hello, {name}
+ {else}
+ Hello, Captain Anonymous
+ {end}
+
+As are else..if clauses:
+
+ {if firstName}
+ Hello, {firstName}
+ {elif lastName}
+ Hello, Mr. {lastName}
+ {else}
+ Hello, Captain Anonymous
+ {end}
+
+## Looping over an array
+
+ {foreach item in someArray}
+ <p>{item}</p>
+ {end}
+
+# Performance
+
+Although it's not really fair to compare Jazz to other template engines out there since
+it's still very early in development, we found the following totally contrived benchmark
+to be promising:
+
+## json-template
+
+ $ cat benchmark-jt.js
+ var jt = require("./json-template");
+
+ var template = jt.Template("Hello, World");
+
+ for (var i = 0; i < 5000000; i++) {
+ template.expand({});
+ }
+
+ $ time node benchmark-jt.js
+
+ real 0m4.903s
+ user 0m4.896s
+ sys 0m0.004s
+
+## jazz
+
+ $ cat benchmark-jazz.js
+ var jazz = require("./jazz");
+ var template = jazz.compile("Hello, World");
+
+ for (var i = 0; i < 5000000; i++) {
+ template.eval({});
+ }
+
+ $ time node benchmark-jazz.js
+
+ real 0m0.683s
+ user 0m0.672s
+ sys 0m0.008s
+
5 TODO
@@ -0,0 +1,5 @@
+* Proper escaping of echo strings
+* Port unit tests to use node's assert.js
+* Fix bug resulting in incorrect columns being assigned to scanned tokens.
+* If parameter values are functions, call them as functions
+* More tests for illegal syntax
@@ -0,0 +1,4 @@
+{foreach person in people}
+Hello, {person}!
+{end}
+
@@ -0,0 +1,7 @@
+var jazz = require("../lib/jazz");
+var sys = require("sys");
+var fs = require("fs");
+
+var data = fs.readFileSync(__dirname + "/foreach.jazz");
+var template = jazz.compile(data);
+sys.puts(template.eval({"people": ["Tom", "Danny", "Steve"]}));
@@ -0,0 +1,4 @@
+{foreach email_address in user.email_addresses}
+{email_address}
+{end}
+
@@ -0,0 +1,8 @@
+var jazz = require("../lib/jazz");
+var sys = require("sys");
+var fs = require("fs");
+
+var data = fs.readFileSync(__dirname + "/getattr.jazz");
+var template = jazz.compile(data);
+sys.puts(template.eval({"user": {"email_addresses": ["bob@foo.com", "bleh@blah.com"]}}));
+
@@ -0,0 +1,14 @@
+{if errors}
+<ul>
+ {foreach error in errors}
+ <li>{error}</li>
+ {end}
+</ul>
+{end}
+
+<form method="post" action="/examples/complex">
+ <label for="username">Username:</label>
+ <input id="username" name="username" value="{username}" type="text" />
+ <input type="submit" value="Login" />
+</form>
+
@@ -0,0 +1,14 @@
+var jazz = require("../lib/jazz");
+var sys = require("sys");
+var fs = require("fs");
+
+var data = fs.readFileSync(__dirname + "/html.jazz");
+var template = jazz.compile(data);
+
+sys.puts(template.eval({"username": ""}));
+
+sys.puts(template.eval({
+ "errors": ["Invalid username", "Please try again"],
+ "username": "bert"
+}));
+
@@ -0,0 +1,5 @@
+{if firstVar}firstVar = {firstVar}
+{elif secondVar}secondVar = {secondVar}
+{else}no var :(
+{end}
+
@@ -0,0 +1,21 @@
+var jazz = require("../lib/jazz");
+var sys = require("sys");
+var fs = require("fs");
+
+var data = fs.readFileSync(__dirname + "/ifelse.jazz");
+var template = jazz.compile(data);
+
+//
+// Render the template three times with different inputs
+//
+
+sys.print(template.eval({
+ "firstVar": "MONKEYS!"
+}));
+
+sys.print(template.eval({
+ "secondVar": "GIBBONS!"
+}));
+
+sys.print(template.eval({}));
+
@@ -0,0 +1,13 @@
+Dear {recipient}
+
+{if friendly}
+Hope you're well.
+{end}
+
+It has come to our attention that you owe ${amount} in back fees.
+
+Please pay IMMEDIATELY.
+
+Regards,
+{company}
+
@@ -0,0 +1,12 @@
+var jazz = require("../lib/jazz");
+var sys = require("sys");
+var fs = require("fs");
+
+var data = fs.readFileSync(__dirname + "/letter.jazz");
+var template = jazz.compile(data);
+sys.puts(template.eval({
+ "recipient":"Tom",
+ "friendly":true,
+ "amount":"200.00",
+ "company": "Gimme, Inc."
+}));
@@ -0,0 +1,2 @@
+Hello. I am a template. :)
+
@@ -0,0 +1,8 @@
+var jazz = require("../lib/jazz");
+var sys = require("sys");
+var fs = require("fs");
+
+var data = fs.readFileSync(__dirname + "/simple.jazz");
+var template = jazz.compile(data);
+sys.puts(template.eval({}));
+
@@ -0,0 +1,42 @@
+/*
+ * jazz -- A simple template engine for nodejs
+ * Copyright (C) 2010 Shine Technologies
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+var sys = require("sys");
+var scanner = require("./jazz/scanner");
+var parser = require("./jazz/parser");
+var compiler = require("./jazz/compiler");
+
+exports.createScanner = scanner.createScanner;
+
+exports.createParser = parser.createParser;
+
+exports.createCompiler = compiler.createCompiler;
+
+exports.compile = function(source, options) {
+ options = options || {};
+ var s = scanner.createScanner(source, options["filename"]);
+ var p = parser.createParser(s);
+ p.debug = options["parser:debug"] || false;
+ var c = compiler.createCompiler();
+ c.debug = options["compiler:debug"] || false;
+ var suite = p.parse();
+ var symtable = p.globals;
+ return c.compile(suite, symtable);
+}
+
@@ -0,0 +1,89 @@
+/*
+ * jazz -- A simple template engine for nodejs
+ * Copyright (C) 2010 Shine Technologies
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+function Suite(body) {
+ this.type = "jazz.ast.Suite";
+ this.body = body;
+ return this;
+}
+
+Suite.prototype.toString = function() {
+ return "Suite(" + this.body.map(function(stmt) { return stmt.toString(); }).join(", ") + ")";
+}
+
+function Ident(name) {
+ this.type = "jazz.ast.Ident";
+ this.name = name;
+ return this;
+}
+
+Ident.prototype.toString = function() {
+ return "Ident(" + this.name + ")";
+}
+
+function IfStmt(expr, suite, orelse) {
+ this.type = "jazz.ast.IfStmt";
+ this.expr = expr;
+ this.suite = suite;
+ this.orelse = orelse;
+ return this;
+}
+
+IfStmt.prototype.toString = function() {
+ return "IfStmt(" + this.expr.toString() + ", " + this.suite.toString() + ", " + (this.orelse ? this.orelse.toString() : "null") + ")";
+}
+
+function ForEach(ident, expr, suite) {
+ this.type = "jazz.ast.ForEach";
+ this.ident = ident;
+ this.expr = expr;
+ this.suite = suite;
+ return this;
+}
+
+ForEach.prototype.toString = function() {
+ return "ForEach(" + this.ident.toString() + ", " + this.expr.toString() + ", " + this.suite.toString();
+}
+
+function Echo(value) {
+ this.type = "jazz.ast.EchoStmt";
+ this.value = value;
+ return this;
+}
+
+Echo.prototype.toString = function() {
+ return "Echo(" + this.value.toString() + ")";
+}
+
+function GetAttr(expr, id) {
+ this.expr = expr;
+ this.id = id;
+ return this;
+}
+
+GetAttr.prototype.toString = function() {
+ return "GetAttr(" + this.expr.toString() + ", " + this.id.toString() + ")";
+}
+
+exports.Suite = Suite;
+exports.Ident = Ident;
+exports.IfStmt = IfStmt;
+exports.Echo = Echo;
+exports.ForEach = ForEach;
+exports.GetAttr = GetAttr;
Oops, something went wrong.

0 comments on commit ac19440

Please sign in to comment.