1 | # Haml Coffee Templates [![Build Status](https://secure.travis-ci.org/netzpirat/haml-coffee.png)](http://travis-ci.org/netzpirat/haml-coffee)
|
2 |
|
3 | Haml Coffee is a JavaScript templating solution that uses [Haml](http://haml-lang.com/) as markup, understands inline
|
4 | [CoffeeScript](http://jashkenas.github.com/coffee-script/) and generates a JavaScript function that renders to HTML. It
|
5 | can be used in client-side JavaScript applications that are using
|
6 | [Backbone.js](http://documentcloud.github.com/backbone/), [Spine.js](http://spinejs.com/),
|
7 | [JavaScriptMVC](http://javascriptmvc.com/), [KnockoutJS](http://knockoutjs.com/) and others, or on the server-side in
|
8 | frameworks like [Express](http://expressjs.com/).
|
9 |
|
10 | You can try Haml Coffee online by visiting [Haml Coffee Online](http://haml-coffee-online.herokuapp.com/).
|
11 |
|
12 | ## Installation
|
13 |
|
14 | Haml Coffee is available in NPM and can be installed with:
|
15 |
|
16 | ```bash
|
17 | $ npm install haml-coffee
|
18 | ```
|
19 |
|
20 | Please have a look at the [CHANGELOG](https://github.com/netzpirat/haml-coffee/blob/master/CHANGELOG.md) when upgrading to a
|
21 | newer Haml Coffee version with `npm update`.
|
22 |
|
23 | ## Integration
|
24 |
|
25 | There are different packages available for integrating Haml-Coffee into your workflow:
|
26 |
|
27 | * [grunt-haml](https://github.com/concordusapps/grunt-haml) for projects using [Grunt](http://gruntjs.com/).
|
28 | * [hem-haml-coffee](https://github.com/vojto/hem-haml-coffee) for project using [Hem](https://github.com/maccman/hem/).
|
29 | * [haml_coffee_assets](https://github.com/netzpirat/haml_coffee_assets) for projects using Rails.
|
30 | * [guard-haml-coffee](https://github.com/ouvrages/guard-haml-coffee) for projects using [Guard](https://github.com/guard/guard).
|
31 | * [Haml Coffee compiler](https://raw.github.com/netzpirat/haml-coffee/master/dist/compiler/hamlcoffee.js)
|
32 | ([minified](https://raw.github.com/netzpirat/haml-coffee/master/dist/compiler/hamlcoffee.min.js))
|
33 | for compiling in the browser.
|
34 |
|
35 | The browser distribution doesn't come bundled with CoffeeScript, so you'll have to make sure you've
|
36 | included it before requiring haml-coffee.
|
37 |
|
38 | ## Compile Haml Coffee
|
39 |
|
40 | ### Using the API
|
41 |
|
42 | You can compile a Haml Coffee template to a JavaScript function and execute the function with the locals to render the
|
43 | HTML. The following code
|
44 |
|
45 | ```coffeescript
|
46 | hamlc = require 'haml-coffee'
|
47 | tmpl = hamlc.compile '%h1= @title'
|
48 | html = tmpl title: 'Haml Coffee rocks!'
|
49 | ```
|
50 |
|
51 | will create the HTML `<h1>Haml Coffee rocks!</h1>`.
|
52 |
|
53 | The `compile` function can take the compiler options as second parameter to customize the template function:
|
54 |
|
55 | ```coffeescript
|
56 | hamlc.compile '%h1= @title'
|
57 | cleanValue: false
|
58 | escapeHtml: false
|
59 | ```
|
60 |
|
61 | See the [compiler options](#compiler-options) for detailed information about all the available options and browse
|
62 | the [codo](https://github.com/netzpirat/codo) generated
|
63 | [Haml-Coffee API documentation](http://coffeedoc.info/github/netzpirat/haml-coffee/master/).
|
64 |
|
65 | ### Using with Express
|
66 |
|
67 | You can configure [Express](http://expressjs.com/) to use Haml Coffee as template engine.
|
68 |
|
69 | #### Express 3
|
70 |
|
71 | Starting with version 1.4.0, Haml Coffee has support for Express 3 and can be registered as view engine as follows:
|
72 |
|
73 | ```coffeescript
|
74 | express = require 'express'
|
75 | app = express()
|
76 |
|
77 | app.engine 'hamlc', require('haml-coffee').__express
|
78 | ```
|
79 |
|
80 | Alternatively you can also use [consolidate.js](https://github.com/visionmedia/consolidate.js) to register the engine:
|
81 |
|
82 | ```coffeescript
|
83 | express = require 'express'
|
84 | cons = require 'consolidate'
|
85 | app = express()
|
86 |
|
87 | app.engine 'hamlc', cons['haml-coffee']);
|
88 | ```
|
89 |
|
90 | #### Express 2
|
91 |
|
92 | Starting with version 0.5.0, Haml Coffee has support for Express 2 and can be registered as view engine as follows:
|
93 |
|
94 | ```coffeescript
|
95 | express = require 'express'
|
96 |
|
97 | app = express.createServer()
|
98 | app.register '.hamlc', require('haml-coffee')
|
99 | ```
|
100 |
|
101 | Alternatively you can also use [consolidate.js](https://github.com/visionmedia/consolidate.js) to register the engine:
|
102 |
|
103 | ```coffeescript
|
104 | express = require 'express'
|
105 | cons = require 'consolidate'
|
106 |
|
107 | app = express.createServer()
|
108 | app.register '.hamlc', cons['haml-coffee']
|
109 | ```
|
110 |
|
111 | #### Express Usage
|
112 |
|
113 | ##### Layouts
|
114 |
|
115 | Express 2 uses a layout file `layout.hamlc` by default and you have to insert the rendered view body into the layout like
|
116 | this:
|
117 |
|
118 | ```haml
|
119 | !!!
|
120 | %head
|
121 | %title Express App
|
122 | %body
|
123 | != @body
|
124 | ```
|
125 |
|
126 | Now you can create a Haml Coffee view
|
127 |
|
128 | ```haml
|
129 | %h1= "Welcome #{ @name }"
|
130 | %p You've rendered your first Haml Coffee view.
|
131 | ```
|
132 |
|
133 | that you can render with:
|
134 |
|
135 | ```coffeescript
|
136 | app.get '/', (req, res) ->
|
137 | res.render 'index.hamlc', name: 'Express user'
|
138 | ```
|
139 |
|
140 | Express 3 has removed layout support, but you can get it back by installing
|
141 | [express-partials](https://github.com/publicclass/express-partials) and configure it as middleware:
|
142 |
|
143 | ```
|
144 | partials = require 'express-partials'
|
145 | app.use partials()
|
146 | ```
|
147 |
|
148 | ##### Default template engine
|
149 |
|
150 | It's possible to use Haml Coffee as the default template engine by setting the `view engine`:
|
151 |
|
152 | ```coffeescript
|
153 | app.configure ->
|
154 | app.set 'view engine', 'hamlc'
|
155 | ```
|
156 |
|
157 | which allows you to omit the `.hamlc` extension when rendering a template:
|
158 |
|
159 | ```coffeescript
|
160 | app.get '/', (req, res) ->
|
161 | res.render 'index', name: 'Express user'
|
162 | ```
|
163 |
|
164 | ##### Compiler options
|
165 |
|
166 | With Express 3, you can set global compiler options by using `app.locals`:
|
167 |
|
168 | ```
|
169 | app.locals.uglify = true
|
170 | ```
|
171 |
|
172 | which is the same as:
|
173 |
|
174 | ```
|
175 | res.render view, { uglify: true }
|
176 | ```
|
177 |
|
178 | See the [compiler options](#compiler-options) for detailed information about all the available options.
|
179 |
|
180 | ### Using the CLI tool
|
181 |
|
182 | After the installation you will have a `haml-coffee` binary that can be used to compile single templates and even
|
183 | compile multiple templates recursively into a single file.
|
184 |
|
185 | ```bash
|
186 | $ haml-coffee
|
187 | Usage: node haml-coffee
|
188 |
|
189 | Options:
|
190 | -i, --input Either a file or a directory name to be compiled
|
191 | -o, --output Set the output filename
|
192 | -n, --namespace Set a custom template namespace
|
193 | -t, --template Set a custom template name
|
194 | -b, --basename Ignore file path when generate the template name
|
195 | -e, --extend Extend the template scope with the context
|
196 | ```
|
197 |
|
198 | _The following section describes only the options that are unique to the command line tool._
|
199 |
|
200 | You can see all the available options by executing `haml-coffee --help` and have a look at the
|
201 | [compiler options](#compiler-options) for detailed information about all the options.
|
202 |
|
203 | The `input` and `output` are optional and you can also directly redirect the streams.
|
204 |
|
205 | #### Input filename
|
206 |
|
207 | You can either specify a single template or a directory with the `-i`/`--input` argument. When you supply a directory,
|
208 | templates are being searched recursively:
|
209 |
|
210 | ```bash
|
211 | $ haml-coffee -i template.haml
|
212 | ```
|
213 |
|
214 | This will generate a template with the same name as the file but the extension changed to `.jst`. The above command for
|
215 | example would generate a template named `template.jst`.
|
216 |
|
217 | A valid Haml Coffee template must have one of the following extensions: `.haml`, `.html.haml`, `.hamlc` or
|
218 | `.html.hamlc`.
|
219 |
|
220 | #### Output filename
|
221 |
|
222 | You can specify a single output file name to be used instead of the automatic generated output file name with the
|
223 | `-o`/`--output` argument:
|
224 |
|
225 | ```bash
|
226 | $ haml-coffee -i template.haml -o t.js
|
227 | ```
|
228 |
|
229 | This creates a template named `t.js`. You can also set a directory as input and give an output file name for
|
230 | concatenating all templates into a single file:
|
231 |
|
232 | ```bash
|
233 | $ haml-coffee -i templates -o all.js
|
234 | ```
|
235 |
|
236 | This will create all the templates under the `templates` directory into a single, combined output file `all.js`.
|
237 |
|
238 | #### Template namespace
|
239 |
|
240 | Each template will register itself by default under the `window.HAML` namespace, but you can change the namespace with
|
241 | the `-n`/`--namespace` argument:
|
242 |
|
243 | ```bash
|
244 | $ haml-coffee -i template.haml -n exports.JST
|
245 | ```
|
246 |
|
247 | #### Template name
|
248 |
|
249 | Each template must have a unique name under which it can be addressed. By default the template name is derived from the
|
250 | template file name by stripping off all extensions and remove illegal characters. Directory names are converted to
|
251 | nested namespaces under the default namespace. For example, a template named `user/show-admin.html.haml` will result in
|
252 | a template that can be accessed by `window.HAML['user/show_admin']`.
|
253 |
|
254 | Given the `-b`/`--basename` argument, the deduced template name will not include the path to the template. For example,
|
255 | a template named `user/show-admin.html.haml` will result in a template that can be accessed by
|
256 | `window.HAML['show_admin']` instead of `window.HAML['user/show_admin']`.
|
257 |
|
258 | With the `-t`/`--template` argument you can set a template name manually:
|
259 |
|
260 | ```bash
|
261 | $ haml-coffee -i template.haml -n exports.JST -t other
|
262 | ```
|
263 |
|
264 | This will result in a template that can be accessed by `exports.JST['other']`.
|
265 |
|
266 | #### Extend the template scope
|
267 |
|
268 | By extending the template scope with the context, you can access your context data without `@` or `this`:
|
269 |
|
270 | ```Haml
|
271 | %h2= title
|
272 | ```
|
273 |
|
274 | This effect is achieved by using the [with](https://developer.mozilla.org/en/JavaScript/Reference/Statements/with)
|
275 | statement. Using with is forbidden in ECMAScript 5 strict mode.
|
276 |
|
277 | #### Stream redirection
|
278 |
|
279 | You can use Haml Coffee on the command line to enter a template and stop it with `Ctrl-D`:
|
280 |
|
281 | ```bash
|
282 | $ haml-coffee -p amd
|
283 | %h1 Hello AMD
|
284 | ^D
|
285 | ```
|
286 |
|
287 | which will output the AMD module source code to the console. You either have to set the placement option to `amd` or
|
288 | give it a template name like
|
289 |
|
290 | ```bash
|
291 | $ haml-coffee -t name
|
292 | %p JST rocks!
|
293 | ^D
|
294 | ```
|
295 |
|
296 | which will output the JST source code. Now you can also redirect files like:
|
297 |
|
298 | ```bash
|
299 | $ haml-coffee -t name < input.hamlc > output.jst
|
300 | ```
|
301 |
|
302 | ## Haml support
|
303 |
|
304 | Haml Coffee implements the [Haml Spec](https://github.com/norman/haml-spec) to ensure some degree of compatibility to
|
305 | other Haml implementations and the following sections are fully compatible to Ruby Haml:
|
306 |
|
307 | * Plain text
|
308 | * Multiline: `|`
|
309 | * Element names `%`
|
310 | * Attributes: `{}` or `()`
|
311 | * Class and ID: `.` and `#`, implicit `div` elements
|
312 | * Self-closing tags: `/`
|
313 | * Doctype: `!!!`
|
314 | * HTML comments: `/`, conditional comments: `/[]`, Haml comments: `-#`
|
315 | * Running CoffeeScript: `-`, inserting CoffeeScript: `=`
|
316 | * CoffeeScript interpolation: `#{}`
|
317 | * Whitespace preservation: `~`
|
318 | * Whitespace removal: `>` and `<`
|
319 | * Escaping `\`
|
320 | * Escaping HTML: `&=`, unescaping HTML: `!=`
|
321 | * Filters: `:plain`, `:javascript`, `:css`, `:cdata`, `:escaped`, `:preserve`
|
322 | * Boolean attributes conversion
|
323 | * Haml object reference syntax: `[]`
|
324 |
|
325 | Please consult the official [Haml reference](http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html) for more
|
326 | details.
|
327 |
|
328 | Haml Coffee supports both Ruby 1.8 and Ruby 1.9 style attributes. So the following Ruby 1.8 style attribute
|
329 |
|
330 | ```haml
|
331 | %a{ :href => 'http://haml-lang.com/', :title => 'Haml home' } Haml
|
332 | ```
|
333 |
|
334 | can also be written in Ruby 1.9 style:
|
335 |
|
336 | ```haml
|
337 | %a{ href: 'http://haml-lang.com/', title: 'Haml home' } Haml
|
338 | ```
|
339 |
|
340 | HTML style tags are also supported:
|
341 |
|
342 | ```haml
|
343 | %a( href='http://haml-lang.com/' title='Haml home') Haml
|
344 | ```
|
345 |
|
346 | ### Helpers
|
347 |
|
348 | Haml Coffee supports a small subset of the Ruby Haml [helpers](http://haml-lang.com/docs/yardoc/Haml/Helpers.html). The
|
349 | provided helpers will bind the helper function to the template context, so it isn't necessary to use `=>`.
|
350 |
|
351 | #### Surround
|
352 |
|
353 | Surrounds a block of Haml code with strings, with no whitespace in between.
|
354 |
|
355 | ```haml
|
356 | != surround '(', ')', ->
|
357 | %a{:href => "food"} chicken
|
358 | ```
|
359 |
|
360 | produces the HTML output
|
361 |
|
362 | ```html
|
363 | (<a href='food'>chicken</a>)
|
364 | ```
|
365 |
|
366 | #### Succeed
|
367 |
|
368 | Appends a string to the end of a Haml block, with no whitespace between.
|
369 |
|
370 | ```haml
|
371 | click
|
372 | != succeed '.', ->
|
373 | %a{:href=>"thing"} here
|
374 | ```
|
375 |
|
376 | produces the HTML output
|
377 |
|
378 | ```html
|
379 | click
|
380 | <a href='thing'>here</a>.
|
381 | ```
|
382 |
|
383 | #### Precede
|
384 |
|
385 | Prepends a string to the beginning of a Haml block, with no whitespace between.
|
386 |
|
387 | ```haml
|
388 | != precede '*', ->
|
389 | %span.small Not really
|
390 | ```
|
391 |
|
392 | produces the HTML output
|
393 |
|
394 | ```html
|
395 | *<span class='small'>Not really</span>
|
396 | ```
|
397 |
|
398 | ### Object reference: `[]`
|
399 |
|
400 | Haml-Coffee supports object references, but they are implemented slightly different due to the underlying runtime and
|
401 | different code style for CoffeeScript.
|
402 |
|
403 | Square brackets contain a CoffeeScript object or class that is used to set the class and id of that tag. The class is
|
404 | set to the object’s constructor name (transformed to use underlines rather than camel case) and the id is set to the
|
405 | object’s constructor name, followed by the value of its `id` property or its `#to_key` or `#id` functions (in that
|
406 | order). Additionally, the second argument (if present) will be used as a prefix for both the id and class attributes.
|
407 |
|
408 | For example:
|
409 |
|
410 | ```haml
|
411 | %div[@user, 'greeting']
|
412 | Hello
|
413 | ```
|
414 |
|
415 | is compiled to:
|
416 |
|
417 | ```html
|
418 | <div class='greeting_user' id='greeting_user_15'>
|
419 | Hello!
|
420 | </div>
|
421 | ```
|
422 |
|
423 | If the user object is for example a Backbone model with the id of 15. If you require that the class be something other
|
424 | than the underscored object’s constructor name, you can implement the `#hamlObjectRef` function on the object:
|
425 |
|
426 | ```haml
|
427 | :coffeescript
|
428 | class User
|
429 | id: 23
|
430 | hamlObjectRef: -> 'custom'
|
431 |
|
432 | %div[new User()]
|
433 | Hello
|
434 | ```
|
435 |
|
436 | is compiled to:
|
437 |
|
438 | ```html
|
439 | <div class='custom' id='custom_23'>
|
440 | Hello!
|
441 | </div>
|
442 | ```
|
443 |
|
444 | ## CoffeeScript support
|
445 |
|
446 | Haml and CoffeeScript are a winning team, both use indention for blocks and are a perfect match for this reason. You can
|
447 | use CoffeeScript instead of Ruby in your Haml tags and the attributes.
|
448 |
|
449 | **It's not recommended to put too much logic into the template.**
|
450 |
|
451 | ### Attributes
|
452 |
|
453 | When you define an attribute value without putting it into quotes (single or double quotes), it's considered to be
|
454 | CoffeeScript code to be run at render time. By default, attributes values from CoffeeScript code are escaped before
|
455 | inserting into the document. You can change this behaviour by setting the appropriate compiler option.
|
456 |
|
457 | HTML style attributes are the most limited and can only assign a simple variable:
|
458 |
|
459 | ```haml
|
460 | %img(src='/images/demo.png' width=@width height=@height alt=alt)
|
461 | ```
|
462 |
|
463 | Both the `@width` and `@height` values must be passed as locals when rendering the template and `alt` must be defined
|
464 | before the `%img` tag.
|
465 |
|
466 | Ruby style tags can be more complex and can call functions:
|
467 |
|
468 | ```haml
|
469 | %header
|
470 | %user{ :class => App.currentUser.get('status') }= App.currentUser.getDisplayName()
|
471 | ```
|
472 |
|
473 | Attribute definitions are also supported in the Ruby 1.9 style:
|
474 |
|
475 | ```haml
|
476 | %header
|
477 | %user{ class: App.currentUser.get('status') }= App.currentUser.getDisplayName()
|
478 | ```
|
479 |
|
480 | More fancy stuff can be done when use interpolation within a double quoted attribute value:
|
481 |
|
482 | ```haml
|
483 | %header
|
484 | %user{ class: "#{ if @user.get('roles').indexOf('admin') is -1 then 'normal' else 'admin' }" }= @user.getDisplayName()
|
485 | ```
|
486 |
|
487 | _But think twice about it before putting such fancy stuff into your template, there are better places like models,
|
488 | views or helpers to put heavy logic into._
|
489 |
|
490 | You can define your attributes over multiple lines and the next line must not be correctly indented, so you can align
|
491 | them properly:
|
492 |
|
493 | ```haml
|
494 | %input#password.hint{ type: 'password', name: 'registration[password]',
|
495 | data: { hint: 'Something very important', align: 'left' } }
|
496 | ```
|
497 |
|
498 | In the above example you also see the usage for generating HTML5 data attributes.
|
499 |
|
500 | ### Running Code
|
501 |
|
502 | You can run any CoffeeScript code in your template:
|
503 |
|
504 | ```haml
|
505 | - for project in @projects
|
506 | - if project.visible
|
507 | .project
|
508 | %h1= project.name
|
509 | %p&= project.description
|
510 | ```
|
511 |
|
512 | There are several supported types to run your code:
|
513 |
|
514 | * Run code without insert anything into the document: `-`
|
515 | * Run code and insert the result into the document: `=`
|
516 |
|
517 | All inserted content from running code is escaped by default. You can change this behaviour by setting the appropriate
|
518 | compiler option.
|
519 |
|
520 | There are three variations to run code and insert its result into the document, two of them to change the escaping style
|
521 | chosen in the compile option:
|
522 |
|
523 | * Run code and do not escape the result: `!=`
|
524 | * Run code and escape the result: `&=`
|
525 | * Preserve whitespace when insert the result: `~`
|
526 |
|
527 | Again, please consult the official [Haml reference](http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html) for more
|
528 | details. Haml Coffee implements the same functionality like Ruby Haml, only for CoffeeScript.
|
529 |
|
530 | #### Multiline code blocks
|
531 |
|
532 | Running code must be placed on a single line and unlike Ruby Haml, you cannot stretch a it over multiple lines by
|
533 | putting a comma at the end.
|
534 |
|
535 | However, you can use multiline endings `|` to stretch your code over multiple lines to some extend:
|
536 |
|
537 | ```Haml
|
538 | - links = { |
|
539 | home: '/', |
|
540 | docs: '/docs', |
|
541 | about: '/about' |
|
542 | } |
|
543 |
|
544 | %ul
|
545 | - for name, link of links
|
546 | %li
|
547 | %a{ href: link }= name
|
548 | ```
|
549 |
|
550 | Please note, that since the line is concatenated before the compilation, you cannot omit the curly braces and the
|
551 | commas in the above example, like you'd do in normal CoffeeScript code. Therefore it's recommended to use the
|
552 | CoffeeScript filter to have real multiline code blocks:
|
553 |
|
554 | ```Haml
|
555 | :coffeescript
|
556 | links =
|
557 | home: '/'
|
558 | docs: '/docs'
|
559 | about: '/about'
|
560 |
|
561 | %ul
|
562 | - for name, link of links
|
563 | %li
|
564 | %a{ href: link }= name
|
565 | ```
|
566 |
|
567 | #### Functions
|
568 |
|
569 | You can also create functions that generate Haml:
|
570 |
|
571 | ```haml
|
572 | - sum = (a, b) ->
|
573 | %div
|
574 | %span= a
|
575 | %span= b
|
576 | %span= a+b
|
577 | = sum(1,2)
|
578 | = sum(3,4)
|
579 | ```
|
580 |
|
581 | or pass generated HTML output through a function for post-processing.
|
582 |
|
583 | ```haml
|
584 | = postProcess ->
|
585 | %a{ href: '/' }
|
586 | ```
|
587 |
|
588 | The content of the `:coffeescript` filter is run when the template is rendered and doesn't output anything into the
|
589 | resulting document. This comes in handy when you have code to run over multiple lines and don't want to prefix each line
|
590 | with `-`:
|
591 |
|
592 | ```haml
|
593 | %body
|
594 | :coffeescript
|
595 | tags = ['CoffeeScript', 'Haml']
|
596 | project = 'Haml Coffee'
|
597 | %h2= project
|
598 | %ul
|
599 | - for tag in tags
|
600 | %li= tag
|
601 | ```
|
602 |
|
603 | ## Compiler options
|
604 |
|
605 | The following section describes all the available compiler options that you can use through the JavaScript API,
|
606 | as Express view option or as argument to the command line utility.
|
607 |
|
608 | The command line arguments may be slightly different. For example instead of passing `--escape-html=false` you have to
|
609 | use the `--disable-html-escaping` argument. You can see a list of all the command line arguments by executing
|
610 | `haml-coffee --help`.
|
611 |
|
612 | ### HTML generation options
|
613 |
|
614 | The HTML options change the way how the generated HTML will look like.
|
615 |
|
616 | #### Output format
|
617 |
|
618 | * Name: 'format'
|
619 | * Type: `String`
|
620 | * Default: `html5`
|
621 |
|
622 | The Haml parser knows different HTML formats to which a given template can be rendered and it must be one of:
|
623 |
|
624 | * xhtml
|
625 | * html4
|
626 | * html5
|
627 |
|
628 | Doctype, self-closing tags and attributes handling depends on this setting. Please consult the official
|
629 | [Haml reference](http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html) for more details.
|
630 |
|
631 | #### Uglify output
|
632 |
|
633 | * Name: `uglify`
|
634 | * Type: `Boolean`
|
635 | * Default: `false`
|
636 |
|
637 | All generated HTML tags are properly indented by default, so the output looks nice. This can be helpful when debugging.
|
638 | You can skip the indention by setting the `uglify` option to false. This save you some bytes and you'll have increased
|
639 | rendering speed.
|
640 |
|
641 | #### HTML escape
|
642 |
|
643 | * Name: `htmlEscape`
|
644 | * Type: `Boolean`
|
645 | * Default: `true`
|
646 |
|
647 | The reserved HTML characters `"`, `'`, `&`, `<` and `>` are converted to their HTML entities by default when they are
|
648 | inserted into the HTML document from evaluated CoffeeScript.
|
649 |
|
650 | You can always change the escaping mode within the template to either force escaping with `&=` or force unescaping with
|
651 | `!=`.
|
652 |
|
653 | #### Attributes escape
|
654 |
|
655 | * Name: `escapeAttributes`
|
656 | * Type: `Boolean`
|
657 | * Default: `true`
|
658 |
|
659 | All HTML attributes that are generated by evaluating CoffeeScript are also escaped by default. You can turn of HTML
|
660 | escaping of the attributes only by setting `escapeAttributes` to false. You can't change this behaviour in the template
|
661 | since there is no Haml markup for this to instruct the compiler to change the escaping mode.
|
662 |
|
663 | #### Clean CoffeeScript values
|
664 |
|
665 | * Name: `cleanValue`
|
666 | * Type: `Boolean`
|
667 | * Default: `true`
|
668 |
|
669 | Every output that is generated from evaluating CoffeeScript code is cleaned before inserting into the document. The
|
670 | default implementation converts `null` or `undefined` values into an empty string and marks real boolean values with a
|
671 | hidden marker character. The hidden marker character is necessary to distinguish between String values like `'true'`,
|
672 | `'false'` and real boolean values `true`, `false` in the markup, so that a boolean attribute conversion can quickly
|
673 | convert these values to the correct HTML5/XHTML/HTML4 representation.
|
674 |
|
675 | #### Preserve whitespace tags
|
676 |
|
677 | * Name: `preserve`
|
678 | * Type: `String`
|
679 | * Default: `textarea,pre`
|
680 |
|
681 | The `preserve` option defines a list of comma separated HTML tags that are whitespace sensitive. Content from these tags
|
682 | must be preserved, so that the indention has no influence on the displayed content. This is simply done by converting
|
683 | the newline characters to their equivalent HTML entity.
|
684 |
|
685 | #### Autoclose tags
|
686 |
|
687 | * Name: `autoclose`
|
688 | * Type: `String`
|
689 | * Default: `meta,img,link,br,hr,input,area,param,col,base`
|
690 |
|
691 | The autoclose option defines a list of tag names that should be automatically closed if they have no content.
|
692 |
|
693 | #### Module loader support
|
694 |
|
695 | * Name: `placement`
|
696 | * Type: `String`
|
697 | * Default: `global`
|
698 |
|
699 | The `placement` option defines where the template function is inserted
|
700 | upon compilation.
|
701 |
|
702 | Possible values are:
|
703 |
|
704 | * `global` <br />
|
705 | Inserts the optionally namespaced template function into `window.HAML`.
|
706 |
|
707 | * `amd` <br />
|
708 | Wraps the template function into a `define()` statement to allow async
|
709 | loading via AMD.
|
710 |
|
711 | See AMD support for more information.
|
712 |
|
713 | ### Module dependencies
|
714 |
|
715 | * Name: `dependencies`
|
716 | * Type: `Object`
|
717 | * Default: `{ hc: 'hamlcoffee' }`
|
718 |
|
719 | The `dependencies` option allows you to define the modules that must be required for the AMD template `define` function.
|
720 | The object key will be the function parameter name of the module the object value defines. See AMD support for more
|
721 | information.
|
722 |
|
723 | ### Custom helper function options
|
724 |
|
725 | Haml Coffee provides helper functions for HTML escaping, value cleaning and whitespace preservation, which must be
|
726 | available at render time. By default every generated template function is self-contained and includes all of the helper
|
727 | functions.
|
728 |
|
729 | However you can change the reference to each helper function by providing the appropriate compiler option and there
|
730 | are good reasons to do so:
|
731 |
|
732 | * You want to reduce the template size and provide all the helpers from a central place.
|
733 | * You want to customize a helper function to better fit your needs.
|
734 |
|
735 | To change these functions, simply assign the new function name to one of the following options:
|
736 |
|
737 | * `customHtmlEscape`: Escape the reserved HTML characters into their equivalent HTML entity.
|
738 | * `customPreserve`: Converting newlines into their HTML entity.
|
739 | * `customFindAndPreserve`: Find whitespace sensitive tags and preserve their content.
|
740 | * `customCleanValue`: Clean the value that is returned after evaluating some inline CoffeeScript.
|
741 | * `customSurround`: Surrounds a block of Haml code with strings, with no whitespace in between.
|
742 | * `customSucceed`: Appends a string to the end of a Haml block, with no whitespace between.
|
743 | * `customPrecede`: Prepends a string to the beginning of a Haml block, with no whitespace between.
|
744 | * `customReference`: Creates the Haml object reference.
|
745 |
|
746 | The `customSurround`, `customSucceed` and `customPrecede` are bound to the template context.
|
747 |
|
748 | You can find a default implementation for all these helper functions in
|
749 | [Haml Coffee Assets](https://github.com/netzpirat/haml_coffee_assets/blob/master/vendor/assets/javascripts/hamlcoffee.js.coffee.erb).
|
750 |
|
751 | ## AMD support
|
752 |
|
753 | * Global dependencies
|
754 | * Trivial dependency detection
|
755 |
|
756 | Haml Coffee has built in AMD support by setting the `placement` option to `amd`. This will generate a module definition
|
757 | for the JavaScript template. The `dependencies` options can be used to provide a mapping of module names to parameters.
|
758 | To illustrate this, the default value will result in the following module declaration:
|
759 |
|
760 | ```CoffeeScript
|
761 | define ['hamlcoffee'], (hc) ->
|
762 | ```
|
763 |
|
764 | When the template contains a require call in the form of
|
765 |
|
766 | ```CoffeeScript
|
767 | - require 'module'
|
768 | - require 'deep/nested/other'
|
769 | ```
|
770 |
|
771 | it will be added to the module definition list
|
772 |
|
773 | ```CoffeeScript
|
774 | define ['hamlcoffee', 'module', 'deep/nested/other'], (hc, module, other) ->
|
775 | ```
|
776 |
|
777 | allowing you to render a partial template:
|
778 |
|
779 | ```CoffeeScript
|
780 | != module()
|
781 | != other()
|
782 | ```
|
783 |
|
784 | Of course the require call can have different quotes or parenthesises, allowing you to directly require and render:
|
785 |
|
786 | ```CoffeeScript
|
787 | != require("another/other")()
|
788 | ```
|
789 |
|
790 | ## Development information
|
791 |
|
792 | Haml-Coffee uses [Guard](https://github.com/guard/guard) for development, which you can install Guard with the Ruby
|
793 | [Bundler](http://gembundler.com/):
|
794 |
|
795 | ```bash
|
796 | $ bundle install
|
797 | ```
|
798 |
|
799 | Install the Node modules with [NPM](https://npmjs.org/):
|
800 |
|
801 | ```bash
|
802 | $ npm install
|
803 | ```
|
804 |
|
805 | and run Guard to automatically compile your CoffeeScripts and run the Jasmine specs on file modification:
|
806 |
|
807 | ```bash
|
808 | $ bundle exec guard
|
809 | ```
|
810 |
|
811 | ## Changelog
|
812 |
|
813 | Feel free to take a look at the crispy [changelog](https://github.com/netzpirat/haml-coffee/blob/master/CHANGELOG.md)
|
814 | instead of crawling through the commit history.
|
815 |
|
816 | ## Related projects
|
817 |
|
818 | Haml Coffee in the Rails asset pipeline:
|
819 |
|
820 | * [haml-coffee-assets](https://github.com/netzpirat/haml_coffee_assets)
|
821 |
|
822 | ## Authors
|
823 |
|
824 | * [Michael Kessler](https://github.com/netzpirat) ([@netzpirat](http://twitter.com/#!/netzpirat), [mksoft.ch](https://mksoft.ch))
|
825 | * [Sebastion Deutsch](https://github.com/sebastiandeutsch) ([@sippndipp](http://twitter.com/#!/sippndipp), [9elements](http://9elements.com))
|
826 | * [Jan Varwig](https://github.com/janv) ([@agento](http://twitter.com/#!/agento), [9elements](http://9elements.com))
|
827 |
|
828 | ## Contributors
|
829 |
|
830 | See all contributors on [the contributor page](https://github.com/netzpirat/haml-coffee/contributors).
|
831 |
|
832 | ## License
|
833 |
|
834 | (The MIT License)
|
835 |
|
836 | Copyright (c) 2011 9elements, 2011-2013 Michael Kessler
|
837 |
|
838 | Permission is hereby granted, free of charge, to any person obtaining
|
839 | a copy of this software and associated documentation files (the
|
840 | 'Software'), to deal in the Software without restriction, including
|
841 | without limitation the rights to use, copy, modify, merge, publish,
|
842 | distribute, sublicense, and/or sell copies of the Software, and to
|
843 | permit persons to whom the Software is furnished to do so, subject to
|
844 | the following conditions:
|
845 |
|
846 | The above copyright notice and this permission notice shall be
|
847 | included in all copies or substantial portions of the Software.
|
848 |
|
849 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
850 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
851 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
852 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
853 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
854 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
855 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|