Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
thejhh committed Aug 9, 2011
1 parent 624e168 commit 69d2adc
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,76 @@ License

MIT-style license, see [INSTALL.txt](http://github.com/jheusala/js-snippets/blob/master/LICENSE.txt).

Example Code
------------
Example 1 - Single template
---------------------------

File `templates/hello.dust`:

Hello {name}!

File `hello.js`:

var dustfs = require('dustfs');
dustfs.dirs('templates'); // Read templates from this directory
dustfs.render('hello.dust', {'name':'Captain Jack'}, function(err, out) {
if(err) console.log('Error: '+err);
else console.log(out);
});

Results for `node hello.js`:

Hello Captain Jack!

Example 2 - Multiple templates
------------------------------

File `templates/layout.dust`:

<body>
<div id="header">
{+header}
Header
{/header}
</div>
<div id="content">
{+content}
Default content.
{/content}
</div>
</body>

File `templates/partial.dust`:

{>"layout.dust"/}

{<header}
Hello, {name}!
{/header}

{<content}
This is our own content.
{/content}

File `partial.js`:

var dustfs = require('dustfs');
dustfs.debug(true); // Enable optional debug using console.log
dustfs.dirs('templates'); // Read templates from that sub directory
dustfs.render('partial.dust', {'name':'Captain Jack'}, function(err, out) {
if(err) console.log('Error: '+err);
else console.log('Output:\n' + out);
});

Results for `node partial.js`:

[dustfs] [partial.dust] Waiting until directory loading is done before rendering...
[dustfs] [layout.dust] Template compiled from templates/layout.dust
[dustfs] [layout.dust] Template loaded: templates/layout.dust
[dustfs] [hello.dust] Template compiled from templates/hello.dust
[dustfs] [hello.dust] Template loaded: templates/hello.dust
[dustfs] [partial.dust] Template compiled from templates/partial.dust
[dustfs] [partial.dust] Template loaded: templates/partial.dust
[dustfs] [partial.dust] Loading done! Let's render!
[dustfs] [partial.dust] Rendering template...
Output:
<body><div id="header">Hello, Captain Jack!</div><div id="content">This is our own content.</div></body>

0 comments on commit 69d2adc

Please sign in to comment.