• Stars
    star
    508
  • Rank 86,941 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 15 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript Haml

Jaml: beautiful HTML generation for JavaScript

Jaml tries to emulate Ruby’s Haml library, making it easy to generate HTML in your JavaScript projects.

Examples

Something Simple

Registering a template is easy:

Jaml.register('simple', function() {
  div(
    h1("Some title"),
    p("Some exciting paragraph text"),
    br(),

    ul(
      li("First item"),
      li("Second item"),
      li("Third item")
    )
  );
});

So is rendering it:

Jaml.render('simple');

Here’s the output (yes, the indentation really is that pretty):

<div>
  <h1>Some title</h1>
  <p>Some exciting paragraph text</p>
  <br />
  <ul>
    <li>First item</li>
    <li>Second item</li>
    <li>Third item</li>
  </ul>
</div>

Templating

Usually we want to inject data into templates – let’s see how to do that:

Jaml.register('product', function(product) {
  div({cls: 'product'},
    h1(product.title),

    p(product.description),

    img({src: product.thumbUrl}),
    a({href: product.imageUrl}, 'View larger image'),

    form(
      label({for: 'quantity'}, "Quantity"),
      input({type: 'text', name: 'quantity', id: 'quantity', value: 1}),

      input({type: 'submit', value: 'Add to Cart'})
    )
  );
});

And now to render it:

//this is the product we will be rendering
var bsg = {
  title      : 'Battlestar Galactica DVDs',
  thumbUrl   : 'thumbnail.png',
  imageUrl   : 'image.png',
  description: 'Best. Show. Evar.'
};

Jaml.render('product', bsg);

Which gives us:

<div class="product">
  <h1>Battlestar Galactica DVDs</h1>
  <p>Best. Show. Evar.</p>
  <img src="thumbnail.png" />
  <a href="image.png">View larger image</a>
  <form>
    <label for="quantity">Quantity</label>
    <input type="text" name="quantity" id="quantity" value="1"></input>
    <input type="submit" value="Add to Cart"></input>
  </form>
</div>

Collections and partials

We can reuse templates inside other templates. Here we make a Category template to hold more than one product:

Jaml.register('category', function(category) {
  div({cls: 'category'},
    h1(category.name),
    p(category.products.length + " products in this category:"),

    div({cls: 'products'},
      Jaml.render('product', category.products)
    )
  );
});

Now we render it with a couple of products:

//here's a second product
var snowWhite = {
  title      : 'Snow White',
  description: 'not so great actually',
  thumbUrl   : 'thumbnail.png',
  imageUrl   : 'image.png'
};

//and a category
var category = {
  name    : 'Doovde',
  products: [bsg, snowWhite]
}

Jaml.render('category', category);

Which gives us:

<div class="category">
  <h1>Doovde</h1>
  <p>2 products in this category:</p>
  <div class="products"><div class="product">
  <h1>Battlestar Galactica DVDs</h1>
  <p>Best. Show. Evar.</p>
  <img src="thumbnail.png" />
  <a href="image.png">View larger image</a>
  <form>
    <label for="quantity">Quantity</label>
    <input type="text" name="quantity" id="quantity" value="1"></input>
    <input type="submit" value="Add to Cart"></input>
  </form>
</div>
<div class="product">
  <h1>Snow White</h1>
  <p>not so great actually</p>
  <img src="thumbnail.png" />
  <a href="image.png">View larger image</a>
  <form>
    <label for="quantity">Quantity</label>
    <input type="text" name="quantity" id="quantity" value="1"></input>
    <input type="submit" value="Add to Cart"></input>
  </form>
</div>
</div>
</div>

Error handling

If the requested template does not exist, the renderer will return null:

=> Jaml.render('missing');
=> null

Jaml Tests

You can run the Jaml test suite using either node.js at the command line or via a webpage-based runner.

Run the tests in a browser

Jasmine must be checked out in a directory alongside jaml:

git clone https://github.com/pivotal/jasmine.git
ls
=> jaml jasmine

…then open specs/index.html in your browser.

Run the tests using node.js

1) Install node.js.

2) Check out sconover’s jasmine-node alongside jaml:

git clone https://github.com/sconover/jasmine-node.git
ls
=> jaml jasmine-node

3) Run the suite:

$ node specs/suite.js 
Started
..........................

Finished in 0.018 seconds
X tests, Y assertions, 0 failures


More Repositories

1

Ext.ux.Exporter

Exports an Ext.data.Store to Excel or CSV
JavaScript
95
star
2

Ext.ux.Printer

A generic printing class that assists with the printing of any Ext.Component
JavaScript
55
star
3

Sencha.tmbundle

A TextMate bundle for all Sencha products
47
star
4

extjs-solitaire

Solitaire in Ext JS
JavaScript
30
star
5

inform-ai

InformAI allows you to easily retrofit context-aware AI into any React application.
TypeScript
25
star
6

rspec-crud-controller-shared-example-groups

A set of useful shared example groups for testing CRUD controllers with RSpec
Ruby
25
star
7

APOD

Sencha Touch 2 Astronomy Picture Of the Day app
JavaScript
24
star
8

extjs-tmbundle

DEPRECATED - see github.com/edspencer/Sencha.tmbundle instead
18
star
9

SenchaConDemo

This is the code that I miraculously managed to put together live in my MVC Part 1 talk at Sencha Con 2011
JavaScript
17
star
10

ext-applications

A collection of re-usable applications for the Ext desktop
JavaScript
14
star
11

ext-mvc

JavaScript
11
star
12

ext-mvc-rails

JavaScript
9
star
13

react-es6-webpack-starter

Simple starter repo for getting up and running with React, ES6 and webpack
JavaScript
9
star
14

rsc-examples

A collection of examples of how to use React Server Components, using Next JS
TypeScript
9
star
15

extjs-docs

Improvements to the ExtJS API docs application
JavaScript
8
star
16

ObjectBrowser

A Javascript/JSON object browser component for ExtJS 4.x
JavaScript
8
star
17

Bean

OS X Screensaver emulation using Canvas
JavaScript
7
star
18

Ext.ux.RowEditor

Improvements to Ext's RowEditor extension
JavaScript
7
star
19

ext-mvc-baseapp

Starting point for a new ExtJS MVC application
Ruby
7
star
20

ExtMate

JavaScript
6
star
21

extjs-tabscrollermenu

Another way of doing Jay Garcia's excellent Tab Panel scroll menu - just for fun
CSS
6
star
22

tracing-example

An example of how to use distributing tracing to instrument Node JS applications
JavaScript
5
star
23

validates_age

Small and simple plugin for verifying someone's age
Ruby
4
star
24

Weasel

JavaScript implementation of Richard Dawkins' Weasel program
JavaScript
4
star
25

gpt-functions-example

Simple example of how to create, configure and call OpenAI Assistants
TypeScript
4
star
26

extcasts

Source code for Ext JS screencasts (coming early 2009)
JavaScript
3
star
27

stream-multi

TypeScript
3
star
28

admin-crud

Ruby
3
star
29

ext.applyonly

JavaScript
2
star
30

ext-rails-app-generator

Ruby
2
star
31

read-next

ReadNext is a tool that uses AI to create "Read Next" suggestions for your articles. It gives you a simple way to index all your content and automatically generate recommendations for what your audience should read next based on what they're reading right now.
TypeScript
1
star
32

narrator-ai

Generates meta-content like intros, outros and read-nexts so you can focus on the content itself
TypeScript
1
star