Skip to content

Commit

Permalink
forked from ss-hogan 0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
yiwang committed Sep 20, 2012
1 parent ab32279 commit a5daba8
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 297 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,7 @@ node_modules
dump.rdb
npm-debug.log
*.tgz
*.swp
*.swo
*~
!.gitignore
29 changes: 3 additions & 26 deletions HISTORY.md
@@ -1,27 +1,4 @@
0.1.3 / 2012-04-10
0.1.0 / 2012-09-20
==================

* New API for SocketStream 0.3 beta2
* Now includes client code (Hogan VM) for client
* Improved error reporting in console
* Reset repo to remove rubbish


0.1.2 / 2012-03-17
==================

* Templates are now appended to `ss.tmpl` instead of global variables (e.g. `HT`)
* Tip: You may put `window.HT = ss.tmpl` into `entry.js` to avoid changing your code
* Reduced amount of code output for apps with many templates


0.1.1 / 2012-03-17
==================

* Upgraded `hogan` to 2.0.0


0.1.0 / 2012-01-14
==================

* Initial commit
* Initial commit
* Forked from ss-hogan v0.1.3 https://github.com/socketstream/ss-hogan/commit/ab32279bfd57882bb7feeccc66faee3e39843749
23 changes: 11 additions & 12 deletions README.md
@@ -1,43 +1,42 @@
# Hogan Template Engine wrapper for SocketStream 0.3
# Handlebars Template Engine wrapper for SocketStream 0.3

http://twitter.github.com/hogan.js/
http://handlebarsjs.com/

Use pre-compiled Hogan (Mustache-compatible) client-side templates in your app to benefit from increased performance and a smaller client-side library download.
Use pre-compiled Handlebars client-side templates in your app.


### Installation

The `ss-hogan` module is installed by default with new apps created by SocketStream 0.3. If you don't already have it installed, add `ss-hogan` to your application's `package.json` file and then add this line to app.js:
Add ss-handlebars to your application's package.json file and then add this line to app.js:

```javascript
ss.client.templateEngine.use(require('ss-hogan'));
ss.client.templateEngine.use(require('ss-handlebars'));
```

Restart the server. From now on all templates will be pre-compiled and accessibale via the `ss.tmpl` object.

Note: Hogan uses a small [client-side VM](https://raw.github.com/twitter/hogan.js/master/lib/template.js) which renders the pre-compiled templates. This file is included and automatically sent to the client.
Note: Handlebars uses a small [client-side runtime](http://handlebarsjs.com/precompilation.html) which renders the pre-compiled templates. This file is included and automatically sent to the client.


### Usage

E.g. a template placed in

/client/templates/offers/latest.html
/client/templates/offers/latest.hds

Can be rendered in your browser with

```javascript
// assumes var ss = require('socketstream')
var html = ss.tmpl['offers-latest'].render({name: 'Special Offers'})
var html = ss.tmpl['offers-latest']({name: 'Special Offers'})
```


### Options

When experimenting with Hogan, or converting an app from one template type to another, you may find it advantageous to use multiple template engines and confine use of Hogan to a sub-directory of `/client/templates`.
When experimenting with Handlebars, or converting an app from one template type to another, you may find it advantageous to use multiple template engines and confine use of Handlebars to a sub-directory of `/client/templates`.

Directory names can be passed to the second argument as so:

```javascript
ss.client.templateEngine.use(require('ss-hogan'), '/hogan-templates');
```
ss.client.templateEngine.use(require('ss-handlebars'), '/hds-templates');
```
240 changes: 0 additions & 240 deletions client.js

This file was deleted.

26 changes: 13 additions & 13 deletions engine.js
@@ -1,25 +1,25 @@
// Hogan Template Engine wrapper for SocketStream 0.3
// Handlebars Template Engine wrapper for SocketStream 0.3

var fs = require('fs'),
path = require('path'),
hogan = require('hogan.js');
Handlebars = require('handlebars');

exports.init = function(ss, config) {

// Send Hogan VM to the client
var clientCode = fs.readFileSync(path.join(__dirname, 'client.js'), 'utf8');
ss.client.send('lib', 'hogan-template', clientCode);
// Send handlebars runtime to the client
var clientCode = fs.readFileSync(path.join(__dirname, 'handlebars.runtime.js'), 'utf8');
ss.client.send('lib', 'handlebars-template', clientCode);

return {

name: 'Hogan',
name: 'Handlebars',

// Opening code to use when a Hogan template is called for the first time
// Opening code to use when a Handlebars template is called for the first time
prefix: function() {
return '<script type="text/javascript">(function(){var ht=Hogan.Template,t=require(\'socketstream\').tmpl;'
return '<script type="text/javascript">(function(){var ht=Handlebars.template,t=require(\'socketstream\').tmpl;'
},

// Closing code once all Hogan templates have been written into the <script> tag
// Closing code once all Handlebars templates have been written into the <script> tag
suffix: function() {
return '}).call(this);</script>';
},
Expand All @@ -30,15 +30,15 @@ exports.init = function(ss, config) {
var compiledTemplate;

try {
compiledTemplate = hogan.compile(template, {asString: true});
compiledTemplate = Handlebars.precompile(template);
} catch (e) {
var message = '! Error compiling the ' + path + ' template into Hogan';
var message = '! Error compiling the ' + path + ' template into Handlebars';
console.log(String.prototype.hasOwnProperty('red') && message.red || message);
throw new Error(e);
compiledTemplate = '<p>Error</p>';
}

return 't[\'' + id + '\']=new ht(' + compiledTemplate + ');';
return 't[\'' + id + '\']= ht(' + compiledTemplate + ');';
}
}
}
}

0 comments on commit a5daba8

Please sign in to comment.