Gary Chambers avatar Gary Chambers committed fdcde59

Added template builder (candidate replacement for Template module)

Comments (0)

Files changed (1)

src/addons/app/builder.js

+/**
+Template engine with built-in data
+binding hooks.
+
+@class Builder
+@static
+@category addons app
+**/
+Jazz.ns.set("app.Builder", (function( context, document, undefined )
+{
+	"use strict";
+
+	var Builder,
+
+		_convertTokens,
+		_zealousTrim;
+
+	Builder = {};
+
+	Builder.config = Jazz.Storage.load("app:builder:config", {
+		interpolate: /\{\{\s*(.+?)\s*\}\}/g,
+		evaluate: /\{%\s*(.+?)\s*%\}/g
+	});
+
+	Builder.compile = function compile( tmpl )
+	{
+		tmpl = _zealousTrim(tmpl);
+		tmpl = _convertTokens(tmpl);
+		console.log( tmpl );
+	};
+
+	/**
+	Converts logical constructs to
+	their programmatic forms.
+
+	@method _convertTokens
+	@private
+	@param {String} tmpl
+	@return {String}
+	**/
+	_convertTokens = function _convertTokens( tmpl )
+	{
+		return tmpl.replace(Builder.config.get("evaluate"), function( line, match )
+		{
+			var expr,
+				tokens;
+
+			expr = "{% ";
+			tokens = match.split(" ");
+
+			switch( tokens[0] )
+			{
+				case "if":
+				case "foreach":
+				case "with":
+					expr += tokens[0] + " (" + tokens.slice(1).join(" ") + ") {";
+					break;
+
+				case "endif":
+				case "endforeach":
+				case "endwith":
+					expr += "}";
+					break;
+
+				case "else":
+					expr += "} " + tokens[0] + " {";
+					break;
+			}
+
+			expr += " %}";
+
+			return expr;
+		});
+	};
+
+	/**
+	Condenses all whitespace to a single space
+
+	@method _zealousTrim
+	@private
+	@param {String} tmpl
+	@return {String}
+	**/
+	_zealousTrim = function _zealousTrim( tmpl )
+	{
+		return tmpl.replace(/\s\s+/g, " ");
+	};
+
+	return Builder;
+
+})( this, this.document ));
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.