Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any client side haml parser?

I'm looking to render my haml code in client side using JavaScript. There are nice haml parsers in server side like Jade or haml.js but I don't know any haml parser/decoder in client side.

Update: Jade that is pretty much haml now supports client side.

like image 584
Mohsen Avatar asked Dec 12 '11 23:12

Mohsen


1 Answers

After some googling I found the "client-side-haml-js" github project. Looks like it should meet your needs:

The clientside-haml-js is a compiler written in CoffeeScript that compiles text templates in HAML format into Javascript functions that generate HTML. It has been inspired by the server side haml Javascript project, and has been written to be feature compatible with Ruby server side HAML, supports all major browsers (IE 7+, Firefox 3.6+, Chrome 10+, Safari), have minimal runtime dependencies (only underscore.js, underscore.string and CoffeeScript if using CoffeeScript in your templates).

NOTE: The haml compiler requires a browser with a JSON parser. For browsers like IE7, you need to also include a JSON implementation. See http://www.json.org/ for more details. A JSON implementation is available at https://github.com/douglascrockford/JSON-js.

Example from their github page:

var fn = haml.compileStringToJs("%h1\n  %div\n    %p\n    %span");
var html = fn();

Looks like it also supports a text/haml-template method similar to jquery-templates:

<script type="text/haml-template" id="simple">
%h1
  %div
    %p
    %span
</script>

<script type="text/javascript">
    var fn = haml.compileHaml('simple');
    var html = fn();
</script>
like image 140
Jared Avatar answered Oct 11 '22 11:10

Jared