Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 0a6dc040627a8978c48bf0734790fe9ce6ebc50a @mgutz mgutz committed Oct 21, 2012
Showing with 553 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +8 −0 LICENSE
  3. +81 −0 README.md
  4. +33 −0 example/app.js
  5. +7 −0 example/public/css/style.css
  6. +17 −0 example/views/fruits/index.hbs
  7. +26 −0 example/views/index.hbs
  8. +17 −0 example/views/layout/default.hbs
  9. +5 −0 example/views/partials/scripts.html
  10. +1 −0 index.js
  11. +100 −0 lib/async.js
  12. +233 −0 lib/hbs.js
  13. +22 −0 package.json
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules
+npm-debug.log
@@ -0,0 +1,8 @@
+The MIT License (MIT)
+Copyright (c) 2012 barc.com
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,81 @@
+# express-hbs
+
+Express 3 handlebars template engine complete with multiple layouts, blocks and cached partials.
+
+Open source project from [Barc](http://barc.com), instant real-time forum on any website.
+
+## Usage
+
+To use with express 3.
+
+```javascript
+var hbs = require('express-hbs');
+
+// Hook in express-hbs and tell it to use hbs for extensions and use `views/partials`
+// for partials.
+app.engine('hbs', hbs.express3({partialsDir: __dirname + '/views/partials'}));
+app.set('view engine', 'hbs');
+app.set('views', __dirname + '/views');
+```
+
+## Syntax
+
+To mark where page should be inserted into a layout.
+
+ {{{body}}}
+
+To use a layout, use handlebars comment. `LAYOUT` is a relative path from template.
+
+ {{!< LAYOUT}}
+
+To define a block in layout.
+
+ {{{block "pageScripts"}}}
+
+To define block content in a page.
+
+ {{contentFor "pageScripts"}}
+ CONTENT HERE
+ {{/contentFor}}
+
+
+## Example
+
+
+File `views/layout/default.hbs`
+
+```html
+<html>
+ <head>
+ <title>{{title}}</title>
+ <link type="text/css" rel="stylesheet" href="/css/style.css"/>
+ {{{block "pageStyles"}}}
+ </head>
+ <body>
+ {{{body}}}
+
+ {{> scripts}}
+
+ {{{block "pageScripts"}}}
+ </body>
+</html>
+```
+
+
+File `views/index.hbs`
+
+```html
+{{!< layout/default}}
+
+{{#contentFor 'pageStyles'}}
+<style>
+ .clicker {
+ color: blue;
+ };
+</style>
+{{/contentFor}}
+
+
+<h1>{{title}}</h1>
+<p class='clicker'>Click me!</p>
+```
@@ -0,0 +1,33 @@
+// npm install express express-hbs
+
+var express = require('express');
+var app = express();
+var hbs = require('..'); // should be `require('express-hbs')` outside of this example
+
+app.use(express.static(__dirname + '/public'));
+
+// Hook in express-hbs and tell it where partials are found
+app.engine('hbs', hbs.express3({partialsDir: __dirname + '/views/partials'}));
+app.set('view engine', 'hbs');
+app.set('views', __dirname + '/views');
+
+var fruits = [];
+fruits.push({ name: 'apple' });
+fruits.push({ name: 'orange' });
+fruits.push({ name: 'pear' });
+
+app.get('/', function(req, res){
+ res.render('index', {
+ title: 'express-hbs example'
+ });
+});
+
+app.get('/fruits', function(req, res){
+ res.render('fruits/index', {
+ title: 'My favorite fruits',
+ fruits: fruits
+ });
+});
+
+app.listen(3000);
+console.log('Express server listening on port 3000');
@@ -0,0 +1,7 @@
+* {
+ font-family: Georgia, serif;
+}
+
+h1, h2 {
+ font-family: Helvetica, Arial, sans;
+}
@@ -0,0 +1,17 @@
+{{!< ../layout/default}}
+
+{{#contentFor "pageStyles"}}
+<style>
+ li {
+ color: #800;
+ }
+</style>
+{{/contentFor}}
+
+
+<h1>{{title}}</h1>
+<ul>
+ {{#each fruits}}
+ <li>{{this.name}}</li>
+ {{/each}}
+</ul>
@@ -0,0 +1,26 @@
+<!-- Relative path to layout -->
+{{!< layout/default}}
+
+{{#contentFor 'pageStyles'}}
+<style>
+ .clicker {
+ color: blue;
+ };
+</style>
+{{/contentFor}}
+
+
+{{#contentFor 'pageScripts'}}
+<script>
+ $(function() {
+ $('p').click(function() {
+ $(this).text('Hello express-hbs!');
+ });
+ });
+</script>
+{{/contentFor}}
+
+
+<h1>{{title}}</h1>
+<p class='clicker'>Click me!</p>
+<a href='/fruits'>Favorite fruits</a>
@@ -0,0 +1,17 @@
+<html>
+ <head>
+ <title>{{title}}</title>
+ <link type="text/css" rel="stylesheet" href="/css/style.css"/>
+ {{{block "pageStyles"}}}
+ </head>
+ <body>
+ <!-- Placeholder for page content. -->
+ {{{body}}}
+
+ <!-- Partials are common snippets to be used 1 or more pages. -->
+ {{> scripts}}
+
+ <!-- A block is placeholder for pieces of content defined in a page. -->
+ {{{block "pageScripts"}}}
+ </body>
+</html>
@@ -0,0 +1,5 @@
+{{#if production}}
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
+{{else}}
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js"></script>
+{{/if}}
@@ -0,0 +1 @@
+module.exports = require('./lib/hbs');
@@ -0,0 +1,100 @@
+/// provides the async helper functionality
+
+// global baton which contains the current
+// set of deferreds
+var waiter;
+
+function Waiter() {
+ var self = this;
+
+ // found values
+ self.values = {};
+
+ // callback when done
+ self.callback = null;
+
+ self.resolved = false;
+
+ self.count = 0;
+};
+
+Waiter.prototype.wait = function() {
+ var self = this;
+ ++self.count;
+};
+
+// resolve the promise
+Waiter.prototype.resolve = function(name, val) {
+ var self = this;
+
+ self.values[name] = val;
+
+ // done with all items
+ if (--self.count === 0) {
+ self.resolved = true;
+
+ // we may not have a done callback yet
+ if (self.callback) {
+ self.callback(self.values);
+ }
+ }
+};
+
+// sets the done callback for the waiter
+// notifies when the promise is complete
+Waiter.prototype.done = function(fn) {
+ var self = this;
+
+ self.callback = fn;
+ if (self.resolved) {
+ fn(self.values);
+ }
+};
+
+// callback fn when all async helpers have finished running
+// if there were no async helpers, then it will callback right away
+Waiter.done = function(fn) {
+
+ // no async things called
+ if (!waiter) {
+ return fn({});
+ }
+
+ waiter.done(fn);
+
+ // clear the waiter for the next template
+ waiter = undefined;
+};
+
+Waiter.resolve = function(fn, context) {
+ // we want to do async things, need a waiter for that
+ if (!waiter) {
+ waiter = new Waiter();
+ }
+
+ var id = '__' + gen_id() + '__';
+
+ var cur_waiter = waiter;
+ waiter.wait();
+
+ fn(context, function(res) {
+ cur_waiter.resolve(id, res);
+ });
+
+ // return the id placeholder
+ // this will be replaced later
+ return id;
+};
+
+var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_';
+
+var gen_id = function() {
+ var res = '';
+ for (var i=0 ; i<8 ; ++i) {
+ res += alphabet[Math.floor(Math.random() * alphabet.length)];
+ }
+
+ return res;
+};
+
+module.exports = Waiter;
Oops, something went wrong.

0 comments on commit 0a6dc04

Please sign in to comment.