パッケージの詳細

hamljs

visionmedia194.3k0.6.2

Faster / Express compliant Haml implementation

readme

Haml.js

High performance JavaScript Haml implementation for nodejs

For a higher quality implementation you may want to look at my Jade template engine, however the syntax is slightly different. Jade's engine may be back-ported to haml.js in the future.

Build Status

Installation

  $ npm install hamljs

  node> require('hamljs')

Express

To use with Express 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 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:

<div>text</div>

Classes

%div.article.first
  article text here
  and here

html:

<div class="article first">
  article text here and here
</div>

Div Class Shortcut

.comment hey

html:

<div class="comment">hey</div>

Div Id Shortcut

#article-1 foo

html:

<div id="article-1">foo</div>

Combining Ids and Classes

You may chain id and classes in any order:

.article#first.summary content

html:

<div id="first" class="article summary">content</div>

Attributes

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

html:

<a href="http://google.com" title="Google It">Google</a>

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:

<label for="something"></label>

Boolean Attributes

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

html:

<input type="checkbox" checked="checked"/>

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:

<a href="http://google.com" title="Google It" class="button first">Google</a>

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:

= "<br/>"

html:

&lt;br/&gt;

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

!= "<br/>"

html:

<br/>

Iteration

%ul
  - each item in items
    %li= item

html:

<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

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:

<ul>
  <li>one(0)</li>
  <li>two(1)</li>
  <li>three(2)</li>
</ul>

Doctypes

Defaults to transitional:

!!!

html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Optionally pass a supported doctype name:

!!! strict

html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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

'5': '<!DOCTYPE html>',
'xml': '<?xml version="1.0" encoding="utf-8" ?>',
'default': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
'strict': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
'frameset': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
'1.1': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
'basic': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">',
'mobile': '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">'

String interpolation

%div Hello #{world}

with locals:

{ world: "World!" }

html:

<div>Hello World!</div>

:cdata

%script
  :cdata
    foo

html:

<script><![CDATA[
foo
]]></script>

:javascript

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

html:

<head>
  <script type="javascript">
  //<![CDATA[
  if (foo)
    if (bar)
      alert('baz')
  //]]>
  </script>
</head>

Extending Haml

Adding Filters

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

by registering the filter function _myfilter 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 = '<!DOCTYPE ... >'

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.

更新履歴

0.6.2 / 2014-02-17

  • add support for string interpolation
  • add amd support
  • add .renderFile
  • ability to use both class tag and class attribute at the same time

0.6.1 / 2012-07-26

  • fix global leak of Parser

0.6.0 / 2012-06-25

  • remove engine restriction
  • [fix] Changed require('sys') to require('util') for compatibility with node v0.8
  • require('util') instead of require('sys')
  • Refactor tests to not use require.paths
  • Typechange (from a symlink to a file).

0.5.1 / 2011-03-30

  • Exposing Parser for extension [Derek Hammer]

0.5.0 / 2011-03-04

  • Added .compile() for Express 2.0
  • Fixed issue with to many newlines in output. Closes #24

0.4.5 / 2010-06-04

  • Ignoring stray indent

0.4.4 / 2010-05-27

  • Fixed arbitrary whitespace support

0.4.3 / 2010-05-25

  • Fixed support for CRLF / CR line-endings, converted on input
  • Exporting HamlError

0.4.2 / 2010-05-25

  • Added HamlError
  • Buffer newline indentation. Closes #23
  • Benchmarks with node-bench

0.4.1 / 2010-05-17

  • Fixed "- each" with non-enumerables, no longer throws exception
  • Fixed array iteration

0.4.0 / 2010-05-06

  • Using Function constructor instead of eval()
  • Performance enhanced by faster iteration implementation for "- each"

0.3.1 / 2010-04-26

  • Fixed support for tags with hypens (both namespace / tag name, ex: "fb:login-button")

0.3.0 / 2010-04-16

  • Added xml namespace support
  • Added xml support

0.2.0 / 2010-04-06

  • Added conditional comment support [ciaranj]
  • Fixed; Trimming input string before tokenization
  • Fixed issue requiring quoting of "for" when used in attrs [aheckmann]
  • Fixed; Exposing Haml compilation cache. Closes #12
  • Fixed :javascript "type" attr, now "text/javascript" [aheckmann]

0.1.0 / 2010-03-31

  • Added "cache" option, making haml.js over 90 times faster than haml-js
  • Improved textBlock whitespace replication
  • Fixed empty tags followed by class / ids on new lines. Closes #6

0.0.4 / 2010-03-29

  • Added better error reporting

0.0.3 / 2010-03-29

  • Added "filename" option support to aid in error reporting
  • Added exports.compile() to create intermediate javascript
  • Added make benchmark
  • Changed; caching function templates to increase performance
  • Fixed; ids and classes allowing underscores. Closes #5
  • Fixed outdent issue when \n is not followed by whitespace. Closes #8

0.0.2 / 2010-03-26

  • Added haml.js vs haml-js benchmarks
  • Fixed; commenting :javascript CDATA

0.0.1 / 2010-03-26

  • Initial release