Updated README.

This commit is contained in:
Alan Johnson
2011-08-15 23:14:40 -04:00
parent 3c5cac3f3f
commit 292e32292b
+6 -10
View File
@@ -17,7 +17,7 @@ In general, the syntax of Handlebars.js templates is a superset of Mustache temp
Once you have a template, use the Handlebars.compile method to compile the template into a function. The generated function takes a context argument, which will be used to render the template.
var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
"{{kids/length}} kids:</p>" +
"{{kids.length}} kids:</p>" +
"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
var template = Handlebars.compile(source);
@@ -74,19 +74,19 @@ Handlebars.js adds a couple of additional features to make writing templates eas
### Paths
Handlebars.js supports an extended expression syntax that we call paths. Paths are made up of typical expressions and / characters. Expressions allow you to not only display data from the current context, but to display data from contexts that are descendents and ancestors of the current context.
Handlebars.js supports an extended expression syntax that we call paths. Paths are made up of typical expressions and . characters. Expressions allow you to not only display data from the current context, but to display data from contexts that are descendents and ancestors of the current context.
To display data from descendent contexts, use the `/` character. So, for example, if your data were structured like:
To display data from descendent contexts, use the `.` character. So, for example, if your data were structured like:
var data = {"person": { "name": "Alan" }, company: {"name": "Rad, Inc." } };
you could display the person's name from the top-level context with the following expression:
{{person/name}}
{{person.name}}
Similarly, if already traversed into the person object you could still display the company's name with an expression like `{{../company/name}}`, so:
You can backtrack using `../`. For example, if you've already traversed into the person object you could still display the company's name with an expression like `{{../company.name}}`, so:
{{#person}}{{name}} - {{../company/name}}{{/person}}
{{#person}}{{name}} - {{../company.name}}{{/person}}
would render:
@@ -203,10 +203,6 @@ Known Issues
------------
* Handlebars.js can be cryptic when there's an error while rendering.
Handlebars Contrib
------------------
Alan Johnson, a.k.a. commondream, keeps a repository of useful helpers in the [handlebars-contrib](http://github.com/commondream/handlebars-contrib) repository. Feel free to copy those, or add some of your own.
Handlebars in the Wild
-----------------
* Don Park wrote an Express.js view engine adapter for Handlebars.js called [hbs](http://github.com/donpark/hbs)