Skip to content

Instantly share code, notes, and snippets.

@chbrown
Created October 27, 2011 06:11
Show Gist options
  • Select an option

  • Save chbrown/1318893 to your computer and use it in GitHub Desktop.

Select an option

Save chbrown/1318893 to your computer and use it in GitHub Desktop.
Amulet tut
Hi,
There are a few tests for amulet, yes.
In the tests/ directory, run these:
node spec_test.js --ignore-whitespace
node spec_test.js --ignore-whitespace --extended
I use amulet in a couple of production cases.
One big thing that amulet doesn't have is following the Mu spec for whitespace. But whitespace is rarely important in HTML, which is why I haven't worried about it that much.
Amulet is not a benchmark-driven-developed product. It's probably slower than Mu for basic 'hello world' examples. But it supports yields (layout hierarchies) naturally (without partials on each side -- which is just about as ugly as php), and it can wait for context variables and even layout-yielded template names. It's more in line with the Node.js async mentality than any other template engine I've come across. In one app I assign every user a ticket as soon as they hit the page. But I render/send off 95% of the page before I make the various database calls to check if their existing ticket is recognized, or assign them a new one, and then slip that in a <script> just before the </body> end tag, where amulet has been waiting.
To be honest, I just develop it when I need it to work for my projects, so it doesn't have extensive tests to back it up. And it has some extraneous (but I think they're cool and useful) things, like sections that assign into variables, being able to section-iterate over lists of primitive object by using "_", pipes (so you can say something like {{user_details || JSON.stringify}} ).
I'd say give it a shot. I think I'm pretty settled on most of the public-facing methods, but those do change sometimes. And if you find any gaping holes, let me know, and I'll try to fix them.
My headers look kind of like this:
var amulet = require('amulet');
amulet.root(path.join(__dirname, 'templates'));
This will throw errors if any of your templates (*.mu) have parsing errors.
Then later, call:
// synchronous (res is your http server's response argument, context is some possible empty object, {})
amulet.render(['layout.mu', 'results.mu'], context, res);
// async
var renderer = amulet.init(res).start('layout.mu', context);
// go do something
renderer.extendTemplateStack('annotate.mu');
// or
renderer.extendContext({user: user, ticket: ticket});
// and then make sure to run
renderer.force();
// before you let the renderer go out of scope (force makes it sync -- so it'll finish rendering the templates whether or not you have the appropriate fields in your context)
// also, each of those are methods are chainable.
The best place to look at what tags are supported is in 'scanMustache: function' in lib/parsing.js . There is some cool/unstable/non-standard stuff in there. A lot more than the Mu spec calls for.
I'll probably extend this message into a wiki "Getting started" page, or add it to the readme.
Good luck,
-c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment