having fun with code

Mooml: Mootools markup language (intro)

It was a month ago when I found about Jaml, an excellent small Javascript library created by Ed Spencer which allows generating HTML code using very nice and clean templates. At work we use Mootools in most of our projects, so I thought it would be nice to improve Jaml by taking advantage of the power of Mootools. And this is the result.

Mooml: Mootools markup language

What is Mooml? Mooml is a templating system that allows generating HTML snippets very easily, without all the ugly nested “new Element” calls or string concatenation. With Mooml you can save templates for later use or evaluate snippets on the fly.

Download: http://mootools.net/forge/p/mooml
Source: http://github.com/eneko/mooml

Let’s see an example

Required HTML:

  1. <div class="node">
  2.   <h2>Node Title</h2>
  3.   <p>Node body here.</p>
  4. </div>

Node title and body are variables, so let’s save them on a Json format for now.

  1. var data = {
  2.   title: 'Node Title',
  3.   body: 'Node body here'
  4. }

Using Mootools “new Element” we would have to code something like this:

  1. var el = new Element('div', {'class': 'node'}).adopt([
  2.   new Element('h2', {text: data.title}),
  3.   new Element('p', {text: data.body})
  4. ]);

That code is much better than plain Javascript, but still messy. We could use the new Elements.from included in Mootools More 1.2.4, but that would require us writing the html as a string, which rapidly gets messy concatenating variables, etc.

  1. var el = Elements.from('<div class="node"><h2>' + data.title + '</h2><p>' + data.body + '</p></div>');

With Mooml…

With Mooml, we can create a template for that piece of code, and reuse it later as many times as we need:

  1. Mooml.register('node', function(data) {
  2.   div({'class': 'node'},
  3.     h2(data.title),
  4.     p(data.body)
  5.   )
  6. });
  7. document.body.grab(Mooml.render('node', data));

We can also execute the template directly as a function:

  1. document.body.grab(Mooml.templates.node(data));

Finally, with Mooml we can evaluate templates on the fly, without having to store them for later:

  1. document.body.grab(Mooml.evaluate(function(data) {
  2.   div({'class': 'node'},
  3.     h2(data.title),
  4.     p(data.body)
  5.   )
  6. }, data));

More examples soon :)

Related Posts:

7 Comments to Mooml: Mootools markup language (intro)

  1. March 15, 2010 at 13:30 | Permalink

    Hello there, I’ve started to use your library for our project. It’s really awesome and makes my life much simpler, however there’s one thing that would really benefit a lot of people, if it was supported by the library. Namely, the back-referencing the DOM elements when they are created.

    If i create a complex UI piece with mooml I either have to create all elements that I need to reference in advance, and put them into the template, or look them up after the template was rendered.

    First method breaks the flow, and second one is a waster of CPU cycles, since it has to do more unnecessary DOM traversals.

    So if there was a simple way to back-reference those elements that’d be pretty awesome.

  2. March 15, 2010 at 14:49 | Permalink

    Other thing I’ve added for my project is this:


    Moomlable =
    {
    templates: {},
    register : function(name, code)
    {
    this.templates[name] = function(data)
    {
    return Mooml.evaluate(code, data);
    }.bind(this);
    },

    render: function(name, data)
    {
    return this.templates[name](data);
    }
    }

    This way I can make my UI classes say Extends: Moomlable, so they have their local template storage.

  3. March 16, 2010 at 14:16 | Permalink

    returning a hash would prevent from using stuff like .adopt(this.render()).
    Maybe return such hash by reference, passed into the render as third argument.

    Maybe there can be a special property for each element so you can do something like this:


    this.register("template", function()
    {
    div({'reference':'mydiv'},
    div({'reference':'inner'})
    )
    });

    var ref;

    el = this.render("template",[],ref);

    ref.mydiv.setStyle(...);
    ref.inner.setStyle(...);

    and if render returns an array of elements, then ref could contain array of hashes.

    Another thing i’ve noticed is that when I register the templates, and want to bind class methods to events – i have to do following


    this.register("template", function()
    {
    div({events: click:function(){ ... }.bind(params.self))
    });

    this.render("template". {self: this});

    and if i have to call a template render within another template render it gets even uglier… (for instance if i need to render something in a loop and paste that into outer template. I know that mooml was not meant to be like smarty or something, with loops and whatnot, but passing reference to instance’s this field gets tricky.

  4. March 16, 2010 at 19:46 | Permalink

    I’ve decided to fork it on GitHub to add some of the features I’m looking for…

  5. September 2, 2010 at 05:01 | Permalink

    Salam…
    I like this informatin
    Good post fox

  1. By on January 29, 2010 at 01:54

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info