From b2a27347e8c265f37d1a6f7197efaa11c2414b6b Mon Sep 17 00:00:00 2001 From: Odin Dutton Date: Tue, 6 Dec 2011 14:59:48 +1000 Subject: [PATCH] Add syntax highlighting to the README. --- README.markdown | 180 +++++++++++++++++++++++++++--------------------- 1 file changed, 102 insertions(+), 78 deletions(-) diff --git a/README.markdown b/README.markdown index 03bd01a8..5dfd8260 100644 --- a/README.markdown +++ b/README.markdown @@ -16,21 +16,23 @@ 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 = "

Hello, my name is {{name}}. I am from {{hometown}}. I have " + - "{{kids.length}} kids:

" + - ""; - var template = Handlebars.compile(source); +```js +var source = "

Hello, my name is {{name}}. I am from {{hometown}}. I have " + + "{{kids.length}} kids:

" + + ""; +var template = Handlebars.compile(source); - var data = { "name": "Alan", "hometown": "Somewhere, TX", - "kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]}; - var result = template(data); +var data = { "name": "Alan", "hometown": "Somewhere, TX", + "kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]}; +var result = template(data); - // Would render: - //

Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:

- // +// Would render: +//

Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:

+// +``` Registering Helpers @@ -40,22 +42,23 @@ You can register helpers that Handlebars will use when evaluating your template. Here's an example, which assumes that your objects have a URL embedded in them, as well as the text for a link: - Handlebars.registerHelper('link_to', function(context) { - return "" + context.body + ""; - }); +```js +Handlebars.registerHelper('link_to', function(context) { + return "" + context.body + ""; +}); - var context = { posts: [{url: "/hello-world", body: "Hello World!"}] }; - var source = "" +var context = { posts: [{url: "/hello-world", body: "Hello World!"}] }; +var source = "" - var template = Handlebars.compile(source); - template(context); - - // Would render: - // - // +var template = Handlebars.compile(source); +template(context); +// Would render: +// +// +``` Escaping -------- @@ -78,40 +81,50 @@ Handlebars.js supports an extended expression syntax that we call paths. Paths a 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." } }; +```js +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}} +``` 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: - Alan - Rad, Inc. +``` +Alan - Rad, Inc. +``` ### Strings When calling a helper, you can pass paths or Strings as parameters. For instance: - Handlebars.registerHelper('link_to', function(title, context) { - return "" + title + "" - }); +```js +Handlebars.registerHelper('link_to', function(title, context) { + return "" + title + "" +}); - var context = { posts: [{url: "/hello-world", body: "Hello World!"}] }; - var source = '' +var context = { posts: [{url: "/hello-world", body: "Hello World!"}] }; +var source = '' - var template = Handlebars.compile(source); - template(context); +var template = Handlebars.compile(source); +template(context); - // Would render: - // - // +// Would render: +// +// +``` When you pass a String as a parameter to a helper, the literal String gets passed to the helper function. @@ -121,23 +134,25 @@ gets passed to the helper function. Handlebars.js also adds the ability to define block helpers. Block helpers are functions that can be called from anywhere in the template. Here's an example: - var source = ""; - Handlebars.registerHelper('link', function(context, fn) { - return '' + fn(this) + ''; - }); - var template = Handlebars.compile(source); +```js +var source = ""; +Handlebars.registerHelper('link', function(context, fn) { + return '' + fn(this) + ''; +}); +var template = Handlebars.compile(source); - var data = { "people": [ - { "name": "Alan", "id": 1 }, - { "name": "Yehuda", "id": 2 } - ]}; - template(data); +var data = { "people": [ + { "name": "Alan", "id": 1 }, + { "name": "Yehuda", "id": 2 } + ]}; +template(data); - // Should render: - // +// Should render: +// +``` Whenever the block helper is called it is given two parameters, the argument that is passed to the helper, or the current context if no argument is passed and the compiled contents of the block. Inside of the block helper the value of `this` is the current context, wrapped to include a method named `__get__` that helps translate paths into values within the helpers. @@ -148,37 +163,42 @@ Handlebars when it encounters a partial (`{{> partialName}}`). Partials can either be String templates or compiled template functions. Here's an example: - var source = ""; +```js +var source = ""; - Handlebars.registerPartial('link', '{{name}}') - var template = Handlebars.compile(source); +Handlebars.registerPartial('link', '{{name}}') +var template = Handlebars.compile(source); - var data = { "people": [ - { "name": "Alan", "id": 1 }, - { "name": "Yehuda", "id": 2 } - ]}; +var data = { "people": [ + { "name": "Alan", "id": 1 }, + { "name": "Yehuda", "id": 2 } + ]}; - template(data); - - // Should render: - // +template(data); +// Should render: +// +``` ### Comments You can add comments to your templates with the following syntax: - {{! This is a comment }} +```js +{{! This is a comment }} +``` You can also use real html comments if you want them to end up in the output. -
- {{! This comment will not end up in the output }} - -
+```html +
+ {{! This comment will not end up in the output }} + +
+``` Precompiling Templates @@ -248,11 +268,15 @@ changed. Instead of: - template(context, helpers, partials, [data]) +```js +template(context, helpers, partials, [data]) +``` Use: - template(context, {helpers: helpers, partials: partials, data: data}) +```js +template(context, {helpers: helpers, partials: partials, data: data}) +``` Known Issues ------------