Browse files

first commit

  • Loading branch information...
0 parents commit 64f8c233c04a275367e53260d57202efb94142cb @fat committed Mar 27, 2011
72 README.md
@@ -0,0 +1,72 @@
+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, the usual...
+
+ npm install stache
+
+Now, when you're configing 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 like usual using 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.
+
+
+Layouts and Partial magic!!!
+----------------------------
+
+Stache supports layouts! swaggg. Which means you can have something like this:
+
+ <!-- layout.mustache -->
+ <html>
+ <head>
+ {{>script}}
+ </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.
+
+Double note: Check that partial reference for a script!! If when calling your res.render method you don't explicitly specify a script partial, stache will automatically check your views for a script.mustache to load as a partial. Pretty boss huh?
+
+What? I still don't get it...
+-----------------------------
+
+no worries, check the examples folder player for a fully functional example.
+
+
+shoutout
+--------
+major props to donpark (hbs) && bitdrift && mustache.js
36 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')
56 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;
+}
1 examples/views/fat.mustache
@@ -0,0 +1 @@
+My name is {{name}}, and i wanted to say "{{message}}"
2 examples/views/index.mustache
@@ -0,0 +1,2 @@
+Hellllllo world. yeah.
+{{>img}}
15 examples/views/layout.mustache
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML>
+<html lang="en-us">
+ <head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>{{title}}</title>
+ <script src="/src/vendor/loadrunner/dist/loadrunner.js"></script>
+ <link rel="stylesheet" href="/application.css" type="text/css">
+ </head>
+ <body>
+ {{>nav}}
+ <div id="workspace">
+ {{{yield}}}
+ </div>
+ </body>
+</html>
5 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>
1 index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/stache');
50 lib/stache.js
@@ -0,0 +1,50 @@
+/*! 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 new_regex = function () {
+ return new RegExp("{{(>)([^\\/#\\^]+?)\\1?}}+", "g");
+ };
+
+ var regex = new_regex();
+
+ var tag_replace_callback = function (match, operator, name) {
+ if (operator == '>' && !options.partials[name]) {
+ return fs.readFileSync(views + '/' + name + '.mustache', 'utf-8');
+ }
+ return match;
+ };
+
+ var lines = source.split("\n");
+ for (var i = 0; i < lines.length; i++) {
+ lines[i] = lines[i].replace(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;
13 package.json
@@ -0,0 +1,13 @@
+{
+ "name": "stache",
+ "description": "mustache templating for your express apps",
+ "version": "0.0.2",
+ "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"
+ }
+}
29 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 64f8c23

Please sign in to comment.