An open API service indexing awesome lists of open source software.

https://github.com/tj/haml.js

Faster Haml JavaScript implementation for nodejs
https://github.com/tj/haml.js

Last synced: 9 months ago
JSON representation

Faster Haml JavaScript implementation for nodejs

Awesome Lists containing this project

README

          

# Haml.js

High performance JavaScript [Haml](http://haml-lang.com) implementation for [nodejs](http://nodejs.org)

For a higher quality implementation you may want to look at my [Jade](http://jade-lang.com) template engine,
however the syntax is slightly different. Jade's engine may be back-ported to haml.js in the future.

[![Build Status](https://travis-ci.org/visionmedia/haml.js.png?branch=master)](https://travis-ci.org/visionmedia/haml.js)

## Installation

$ npm install hamljs

node> require('hamljs')

## Express

To use with [Express](http://expressjs.com) and the .haml extension, simply register the engine:

app.engine('.haml', require('hamljs').renderFile);

## About

Benchmarks rendering the same 21 line haml file located at _benchmarks/page.haml_,
shows that this library is nearly **65%** or **3 times** faster than haml-js.

Winner: haml.js
Compared with next highest (haml-js), it's:
65.39% faster
2.89 times as fast
0 order(s) of magnitude faster

Haml.js attempts to comply with the original [Haml](http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html)
implementation as well as possible. There are no magic "plugins" like
found in other JavaScript haml implementations, for example the following
will work just fine:

- if (items)
%ul
- for (var i = 0; i < items.length; ++i)
%li= items[i]

Iteration is the one exception to these magical plugins,
since this is **ugly** in JavaScript, you may also:

- if (items)
%ul
- each item in items
%li= item

## Tags

%div text

html:

text

## Classes

%div.article.first
article text here
and here

html:


article text here and here

## Div Class Shortcut

.comment hey

html:

hey

## Div Id Shortcut

#article-1 foo

html:

foo

## Combining Ids and Classes

You may chain id and classes in any order:

.article#first.summary content

html:

content

## Attributes

%a{ href: 'http://google.com', title: 'Google It' } Google

html:

Google

Attribute keys such as "for" are automatically quoted
by haml.js, so instead of:

%label{ 'for': 'something' }

you should:

%label{ for: 'something' }

which will render:

## Boolean Attributes

%input{ type: 'checkbox', checked: true }

html:

## Combining Attributes, Ids, and Classes

Wemay also contain id and classes before or after:

%a.button{ href: 'http://google.com', title: 'Google It' }.first Google

html:

Google

## Code

Code starting with a hyphen will be executed but
not buffered, where as code using the equals sign
will be buffered:

- a = 1
- b = 2
= a + b

html:

3

HTML buffered with equals sign will **always** be escaped:

= "
"

html:

<br/>

To prevent escaping of HTML entities we can use _!=_:

!= "
"

html:


## Iteration

%ul
- each item in items
%li= item

html:


  • one

  • two

  • three

If you require the key or index of the object
or array during iteration simple append a comma
following another id:

%ul
- each item, index in items
%li= item + '(' + index + ')'

html:


  • one(0)

  • two(1)

  • three(2)

## Doctypes

Defaults to transitional:

!!!

html:

Optionally pass a supported doctype name:

!!! strict

html:

currently supported doctypes, which can be
extended simply by adding values to to _haml.doctypes_.

'5': '',
'xml': '',
'default': '',
'strict': '',
'frameset': '',
'1.1': '',
'basic': '',
'mobile': ''

## String interpolation

%div Hello #{world}

with locals:

{ world: "World!" }

html:

Hello World!

## :cdata

%script
:cdata
foo

html:

<![CDATA[
foo
]]>

## :javascript

%head
:javascript
if (foo)
if (bar)
alert('baz')

html:



//<![CDATA[
if (foo)
if (bar)
alert('baz')
//]]>

## Extending Haml

### Adding Filters

var haml = require('hamljs')
haml.filters.my_filter = function(str) {
return doSomethingWith(str)
}

by registering the filter function _my_filter_ we can now
utilize it within our Haml templates as shown below:
%p
:my_filter
some text
here yay
whoop awesome

### Adding Doctypes

var haml = require('hamljs')
haml.doctypes.foo = ''

Will now allow you to:
!!! foo

## License

(The MIT License)

Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.