Skip to content
Browse files

added stache dependency

  • Loading branch information...
1 parent d14891e commit 40ff7e81ca9f51b59a42459509564663fad13e5a @intelekshual intelekshual committed Jun 25, 2011
View
76 node_modules/stache/README.md
@@ -0,0 +1,76 @@
+Stache
+------
+Stache is mustache.js for your node express apps.
+
+![stache](http://f.cl.ly/items/1C3o2G3a121b1W1h0L1o/draft_lens8690031module75775171photo_1261762807mustacheold.jpg)
+
+Whuuuuuut?
+==========
+
+Getting this junk running is SUPER easy! check the deets below...
+
+setting it up
+-------------
+
+first...
+
+ npm install stache
+
+Now, when you're configuring your express app, just add this little code in somewhere near the top:
+
+ app.set('view engine', 'mustache')
+ app.register(".mustache", require('stache'));
+
+*BOOOM!* you're all set!
+
+how to actually use it
+----------------------
+
+render your views with res.render...
+
+ app.get('/', function (req, res) {
+ res.render("index", {
+ locals: {
+ title: req.params.what
+ },
+ partials: {
+ heading: '<title>{{title}}</title>'
+ }
+ });
+ });
+
+Notice you can pass local vars here as well as partials to your templates.
+
+
+Layouts
+-------
+
+Stache supports layouts! *swaggg!* Which means you can have something like this:
+
+ <!-- layout.mustache -->
+ <html>
+ <head>
+ {{>scripts}}
+ </head>
+ <body>
+ {{{yield}}}
+ </body>
+ </html>
+
+Note: yield is a special local var, which will be replaced automatically by the template you specified with res.render.
+
+
+Partials
+--------
+
+Looking at the example above, check out that partial reference for scripts!! If when calling your res.render method you don't explicitly specify a script partial, stache will automatically check your views directory for a script.mustache to load as a partial. Pretty boss huh?
+
+What? I still don't get it...
+-----------------------------
+
+No worries player, check the examples folder for a fully functional example.
+
+
+shoutout
+--------
+major props to donpark (hbs), bitdrift, and obviously mustache.js
View
36 node_modules/stache/examples/app.js
@@ -0,0 +1,36 @@
+// module depencies
+var express = require('express')
+ , app = express.createServer();
+
+//config
+app.set('view engine', 'mustache')
+app.set("views", __dirname + '/views');
+app.register(".mustache", require('stache'));
+app.use(express.static(__dirname + '/public'));
+
+//routes
+app.get('/', function (req, res) {
+ res.render('index', {
+ locals: {
+ title: 'Welcome'
+ }
+ , partials: {
+ img: '<img src="http://cl.ly/5Wd3/draft_lens8690031module75775171photo_1261762807mustacheold.jpg" />'
+ }
+ });
+});
+
+app.get('/user/:name', function (req, res) {
+ res.render(req.params.name, {
+ locals: {
+ title: req.params.name + '\'s page'
+ , name: req.params.name
+ , message: 'stache it in your stache!'
+ }
+ });
+});
+
+//Run
+app.listen(3000);
+
+console.log('example running on port 3000')
View
56 node_modules/stache/examples/public/application.css
@@ -0,0 +1,56 @@
+/* GENERIC */
+body {
+ font-family:'helvetica neue', helvetica, arial;
+ font-size:14px;
+}
+
+img {
+ display:block;
+}
+
+/* NAVIGATION */
+#navigation {
+ background: #DDEEF6;
+ position:fixed;
+ left:30px;
+ top:10px;
+ border-radius: 8px;
+ list-style: none;
+ padding: 0;
+ font-size:12px;
+}
+
+#navigation .title {
+ font-size:16px;
+ color:#333;
+ border-bottom:1px solid #B8D5E3;
+ padding: 4px 20px 4px 10px;
+ background-color:transparent !important;
+}
+
+#navigation li:nth-child(2) {
+ border-top: 1px solid #fff;
+}
+
+#navigation a {
+ display:block;
+ color: #456;
+ text-shadow: 0 1px 0 #fff;
+ text-decoration:none;
+ padding: 4px 20px 4px 10px;
+}
+
+#navigation li:last-child {
+ border-radius: 0 0 8px 8px;
+}
+
+#navigation li:hover {
+ background:#D1E6F0;
+}
+
+/* WORKSPACE */
+
+#workspace {
+ width:780px;
+ margin: 20px auto;
+}
View
1 node_modules/stache/examples/views/fat.mustache
@@ -0,0 +1 @@
+My name is {{name}}, and i wanted to say "{{message}}"
View
2 node_modules/stache/examples/views/index.mustache
@@ -0,0 +1,2 @@
+Hellllllo world. yeah.
+{{>img}}
View
14 node_modules/stache/examples/views/layout.mustache
@@ -0,0 +1,14 @@
+<!DOCTYPE HTML>
+<html lang="en-us">
+ <head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>{{title}}</title>
+ <link rel="stylesheet" href="/application.css" type="text/css">
+ </head>
+ <body>
+ {{>nav}}
+ <div id="workspace">
+ {{{yield}}}
+ </div>
+ </body>
+</html>
View
5 node_modules/stache/examples/views/nav.mustache
@@ -0,0 +1,5 @@
+<ul id="navigation">
+ <li class="title">Pages</li>
+ <li><a href="/">Welcome</a></li>
+ <li><a href="/user/fat">Hey Fat</a></li>
+</ul>
View
1 node_modules/stache/index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/stache');
View
46 node_modules/stache/lib/stache.js
@@ -0,0 +1,46 @@
+/*! written by @fat
+ * but major props to donpark && bitdrift && mustache.js
+ */
+var stache = {}
+ , fs = require('fs')
+ , mustache = require('mustache')
+ , views = '/';
+
+stache.compile = function (source, options) {
+ views = (options && options.settings && options.settings.views) || './views';
+ if (typeof source == 'string') {
+ return function (options) {
+ options.locals = options.locals || {};
+ options.partials = options.partials || {};
+
+ if (options.body) {
+ options.locals.yield = options.body;
+ }
+
+ var partials_regex = new RegExp("{{([>-])([^\\/#\\^]+?)\\1?}}+", "g");
+
+ var tag_replace_callback = function (match, operator, name) {
+ if (operator == '>' && !options.partials[name]) {
+ return fs.readFileSync(views + '/' + name + (options.extension || '.mustache'), 'utf-8');
+ }
+ return match;
+ };
+
+ var lines = source.split("\n");
+ for (var i = 0; i < lines.length; i++) {
+ lines[i] = lines[i].replace(partials_regex, tag_replace_callback, this);
+ }
+
+ return mustache.to_html(lines.join('\n'), options.locals, options.partials);
+ }
+ } else {
+ return source;
+ }
+};
+
+stache.render = function(template, options) {
+ template = stache.compile(template, options);
+ return template(options);
+};
+
+module.exports = stache;
View
337 node_modules/stache/node_modules/mustache/mustache.js
@@ -0,0 +1,337 @@
+/*
+ * CommonJS-compatible mustache.js module
+ *
+ * See http://github.com/janl/mustache.js for more info.
+ */
+
+/*
+ mustache.js — Logic-less templates in JavaScript
+
+ See http://mustache.github.com/ for more info.
+*/
+
+var Mustache = function() {
+ var Renderer = function() {};
+
+ Renderer.prototype = {
+ otag: "{{",
+ ctag: "}}",
+ pragmas: {},
+ buffer: [],
+ pragmas_implemented: {
+ "IMPLICIT-ITERATOR": true
+ },
+ context: {},
+
+ render: function(template, context, partials, in_recursion) {
+ // reset buffer & set context
+ if(!in_recursion) {
+ this.context = context;
+ this.buffer = []; // TODO: make this non-lazy
+ }
+
+ // fail fast
+ if(!this.includes("", template)) {
+ if(in_recursion) {
+ return template;
+ } else {
+ this.send(template);
+ return;
+ }
+ }
+
+ template = this.render_pragmas(template);
+ var html = this.render_section(template, context, partials);
+ if(in_recursion) {
+ return this.render_tags(html, context, partials, in_recursion);
+ }
+
+ this.render_tags(html, context, partials, in_recursion);
+ },
+
+ /*
+ Sends parsed lines
+ */
+ send: function(line) {
+ if(line != "") {
+ this.buffer.push(line);
+ }
+ },
+
+ /*
+ Looks for %PRAGMAS
+ */
+ render_pragmas: function(template) {
+ // no pragmas
+ if(!this.includes("%", template)) {
+ return template;
+ }
+
+ var that = this;
+ var regex = new RegExp(this.otag + "%([\\w-]+) ?([\\w]+=[\\w]+)?" +
+ this.ctag);
+ return template.replace(regex, function(match, pragma, options) {
+ if(!that.pragmas_implemented[pragma]) {
+ throw({message:
+ "This implementation of mustache doesn't understand the '" +
+ pragma + "' pragma"});
+ }
+ that.pragmas[pragma] = {};
+ if(options) {
+ var opts = options.split("=");
+ that.pragmas[pragma][opts[0]] = opts[1];
+ }
+ return "";
+ // ignore unknown pragmas silently
+ });
+ },
+
+ /*
+ Tries to find a partial in the curent scope and render it
+ */
+ render_partial: function(name, context, partials) {
+ name = this.trim(name);
+ if(!partials || partials[name] === undefined) {
+ throw({message: "unknown_partial '" + name + "'"});
+ }
+ if(typeof(context[name]) != "object") {
+ return this.render(partials[name], context, partials, true);
+ }
+ return this.render(partials[name], context[name], partials, true);
+ },
+
+ /*
+ Renders inverted (^) and normal (#) sections
+ */
+ render_section: function(template, context, partials) {
+ if(!this.includes("#", template) && !this.includes("^", template)) {
+ return template;
+ }
+
+ var that = this;
+ // CSW - Added "+?" so it finds the tighest bound, not the widest
+ var regex = new RegExp(this.otag + "(\\^|\\#)\\s*(.+)\\s*" + this.ctag +
+ "\n*([\\s\\S]+?)" + this.otag + "\\/\\s*\\2\\s*" + this.ctag +
+ "\\s*", "mg");
+
+ // for each {{#foo}}{{/foo}} section do...
+ return template.replace(regex, function(match, type, name, content) {
+ var value = that.find(name, context);
+ if(type == "^") { // inverted section
+ if(!value || that.is_array(value) && value.length === 0) {
+ // false or empty list, render it
+ return that.render(content, context, partials, true);
+ } else {
+ return "";
+ }
+ } else if(type == "#") { // normal section
+ if(that.is_array(value)) { // Enumerable, Let's loop!
+ return that.map(value, function(row) {
+ return that.render(content, that.create_context(row),
+ partials, true);
+ }).join("");
+ } else if(that.is_object(value)) { // Object, Use it as subcontext!
+ return that.render(content, that.create_context(value),
+ partials, true);
+ } else if(typeof value === "function") {
+ // higher order section
+ return value.call(context, content, function(text) {
+ return that.render(text, context, partials, true);
+ });
+ } else if(value) { // boolean section
+ return that.render(content, context, partials, true);
+ } else {
+ return "";
+ }
+ }
+ });
+ },
+
+ /*
+ Replace {{foo}} and friends with values from our view
+ */
+ render_tags: function(template, context, partials, in_recursion) {
+ // tit for tat
+ var that = this;
+
+ var new_regex = function() {
+ return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" +
+ that.ctag + "+", "g");
+ };
+
+ var regex = new_regex();
+ var tag_replace_callback = function(match, operator, name) {
+ switch(operator) {
+ case "!": // ignore comments
+ return "";
+ case "=": // set new delimiters, rebuild the replace regexp
+ that.set_delimiters(name);
+ regex = new_regex();
+ return "";
+ case ">": // render partial
+ return that.render_partial(name, context, partials);
+ case "{": // the triple mustache is unescaped
+ return that.find(name, context);
+ default: // escape the value
+ return that.escape(that.find(name, context));
+ }
+ };
+ var lines = template.split("\n");
+ for(var i = 0; i < lines.length; i++) {
+ lines[i] = lines[i].replace(regex, tag_replace_callback, this);
+ if(!in_recursion) {
+ this.send(lines[i]);
+ }
+ }
+
+ if(in_recursion) {
+ return lines.join("\n");
+ }
+ },
+
+ set_delimiters: function(delimiters) {
+ var dels = delimiters.split(" ");
+ this.otag = this.escape_regex(dels[0]);
+ this.ctag = this.escape_regex(dels[1]);
+ },
+
+ escape_regex: function(text) {
+ // thank you Simon Willison
+ if(!arguments.callee.sRE) {
+ var specials = [
+ '/', '.', '*', '+', '?', '|',
+ '(', ')', '[', ']', '{', '}', '\\'
+ ];
+ arguments.callee.sRE = new RegExp(
+ '(\\' + specials.join('|\\') + ')', 'g'
+ );
+ }
+ return text.replace(arguments.callee.sRE, '\\$1');
+ },
+
+ /*
+ find `name` in current `context`. That is find me a value
+ from the view object
+ */
+ find: function(name, context) {
+ name = this.trim(name);
+
+ // Checks whether a value is thruthy or false or 0
+ function is_kinda_truthy(bool) {
+ return bool === false || bool === 0 || bool;
+ }
+
+ var value;
+ if(is_kinda_truthy(context[name])) {
+ value = context[name];
+ } else if(is_kinda_truthy(this.context[name])) {
+ value = this.context[name];
+ }
+
+ if(typeof value === "function") {
+ return value.apply(context);
+ }
+ if(value !== undefined) {
+ return value;
+ }
+ // silently ignore unkown variables
+ return "";
+ },
+
+ // Utility methods
+
+ /* includes tag */
+ includes: function(needle, haystack) {
+ return haystack.indexOf(this.otag + needle) != -1;
+ },
+
+ /*
+ Does away with nasty characters
+ */
+ escape: function(s) {
+ s = String(s === null ? "" : s);
+ return s.replace(/&(?!\w+;)|["<>\\]/g, function(s) {
+ switch(s) {
+ case "&": return "&amp;";
+ case "\\": return "\\\\";
+ case '"': return '\"';
+ case "<": return "&lt;";
+ case ">": return "&gt;";
+ default: return s;
+ }
+ });
+ },
+
+ // by @langalex, support for arrays of strings
+ create_context: function(_context) {
+ if(this.is_object(_context)) {
+ return _context;
+ } else {
+ var iterator = ".";
+ if(this.pragmas["IMPLICIT-ITERATOR"]) {
+ iterator = this.pragmas["IMPLICIT-ITERATOR"].iterator;
+ }
+ var ctx = {};
+ ctx[iterator] = _context;
+ return ctx;
+ }
+ },
+
+ is_object: function(a) {
+ return a && typeof a == "object";
+ },
+
+ is_array: function(a) {
+ return Object.prototype.toString.call(a) === '[object Array]';
+ },
+
+ /*
+ Gets rid of leading and trailing whitespace
+ */
+ trim: function(s) {
+ return s.replace(/^\s*|\s*$/g, "");
+ },
+
+ /*
+ Why, why, why? Because IE. Cry, cry cry.
+ */
+ map: function(array, fn) {
+ if (typeof array.map == "function") {
+ return array.map(fn);
+ } else {
+ var r = [];
+ var l = array.length;
+ for(var i = 0; i < l; i++) {
+ r.push(fn(array[i]));
+ }
+ return r;
+ }
+ }
+ };
+
+ return({
+ name: "mustache.js",
+ version: "0.3.1-dev",
+
+ /*
+ Turns a template and view into HTML
+ */
+ to_html: function(template, view, partials, send_fun) {
+ var renderer = new Renderer();
+ if(send_fun) {
+ renderer.send = send_fun;
+ }
+ renderer.render(template, view, partials);
+ if(!send_fun) {
+ return renderer.buffer.join("\n");
+ }
+ }
+ });
+}();
+
+exports.name = Mustache.name;
+exports.version = Mustache.version;
+
+exports.to_html = function() {
+ return Mustache.to_html.apply(this, arguments);
+};
View
8 node_modules/stache/node_modules/mustache/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "mustache",
+ "author": "http://mustache.github.com/",
+ "description": "{{ mustache }} in JavaScript — Logic-less templates.",
+ "keywords": ["template"],
+ "version": "0.3.1-dev",
+ "main": "./mustache"
+}
View
13 node_modules/stache/package.json
@@ -0,0 +1,13 @@
+{
+ "name": "stache",
+ "description": "mustache templating for your express apps",
+ "version": "0.0.3",
+ "authors": ["Jacob Thornton <@fat>"],
+ "keywords": ["mustache", "express", "stache"],
+ "main": "./index.js",
+ "engines": { "node": ">= 0.4.1" },
+ "dependencies": {
+ "mustache": ">= 0.2.3",
+ "express": ">= 2.1.0"
+ }
+}
View
29 node_modules/stache/test/test.js
@@ -0,0 +1,29 @@
+var assert = require('assert');
+var stache = require("../");
+
+var options = {
+ locals: {
+ two: '2',
+ four: 'for'
+ },
+ partials: {
+ three: 'tres {{four}}'
+ }
+};
+
+var source = 'one {{two}} {{>three}}';
+
+//just render
+var rendered = stache.render(source, options);
+assert.equal(rendered, 'one 2 tres for');
+
+//compile then render
+var template = stache.compile(source, options);
+var rendered = stache.render(template, options);
+assert.equal(rendered, 'one 2 tres for');
+
+//compile empty object without freaking out
+var rendered = stache.render('feels good man', {});
+assert.equal(rendered, 'feels good man');
+
+console.log('looks good boss.')

0 comments on commit 40ff7e8

Please sign in to comment.
Something went wrong with that request. Please try again.