having fun with code

Mooml 1.1 – No more eval(), say Hi to Mooml.Template and Mooml.Templates

Mooml 1.1 has been finally released today and made available to everybody via Github and Mootools Forge plugin repository.

What is new in Mooml 1.1

Mooml 1.1 has a lot of new features, but these are worth to mention:

  • No more eval()
  • Mooml.Template
  • Mooml.Templates
  • Pass functions as arguments to template functions
  • References to elements with an Id

No more eval()

Although the use of eval() was transparent to the final user, Mooml 1.0.X depended on it to properly generate the dom elements using the template functions like div(), span(), table(), etc. Mooml 1.1 does not use eval() anymore, which prevents issues when compressing the code with compilers like Google Closure Compiler and also increments the overall performance of template rendering.

Mooml.Template

Mooml.Template is the new constructor for Mooml templates. This constructor will allow to create standalone templates like this:

  1. var tpl = new Mooml.Template('mytemplate', function(params) {
  2.   div({ id: params.divId }, params.divContent);
  3. });
  4. document.body.grab(tpl.render({
  5.   divId: 'mydiv',
  6.   divContent: 'Hi there!'
  7. }));
  8. // will create: <div id="mydiv">Hi there!</div>

There is no need to create a Mooml.Template object when using Mooml.register(), which will work as in previous versions of Mooml.

Mooml.Templates

Mooml.Templates is a mixing that can be implemented into other Mootools classes. This will allow these classes to create their own templates internally and use them without the need of registering global templates with Mooml.register(). Here is an example of a Dog class:

  1. var Dog = new Class({
  2.   Implements: [Options, Mooml.Templates],
  3.   options: {
  4.     bark: 'Bark! Bark!'
  5.   },
  6.   initialize: function(options) {
  7.     this.setOptions(options);
  8.     this.register('dog', function(bark) {
  9.      div({ 'class': 'dog'}, bark);
  10.     });
  11.   },
  12.   toElement: function() {
  13.     return this.render('dog', this.options.bark);
  14.   }
  15. });
  16.  
  17. document.body.grab(new Dog()); // <div class="dog">Bark! Bark!</div>
  18. document.body.grab(new Dog({ bark: 'Woof! Woof!' })); // <div class="dog">Woof! Woof!</div>
  19. document.body.grab(new Dog({ bark: 'Arf!' })); // <div class="dog">Arf!</div>

Pass functions as arguments to template functions

With Mooml 1.1, template functions accept functions as arguments. These function can be any function that returns an element, array of elements, string, number or any other argument type supported by Mooml template functions. For example:

  1. var tpl = new Mooml.Template('mytemplate', function(content) {
  2.   div(content);
  3. });
  4. document.body.grab(tpl.render(function() {
  5.   return 'Html <u>returned</u> by a function()'
  6. }));
  7. // will create: <div>Html <u>returned</u> by a function()</div>

References to elements with an Id

Finally, Mooml 1.1 templates can return references to all elements rendered that contain an Id, including elements rendered in nested templates:

  1. var tpl = new Mooml.Template('mytemplate', function(content) {
  2.   div({id:'div1'}, content);
  3.   div({id:'div2'}, content);
  4.   div({id:'div3'}, content);
  5. }, {
  6.   saveElementRefs: true
  7. });
  8. document.body.adopt(tpl.render('some text'));
  9. tpl.elementRefs.div1; // reference to <div id="div1">some text</div>
  10. tpl.elementRefs.div2; // reference to <div id="div2">some text</div>
  11. tpl.elementRefs['div3']; // reference to <div id="div3">some text</div>

Don’t miss the new demo: http://enekoalonso.com/projects/mooml/
Enjoy!

Related Posts:

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

PromoteJS

JavaScript JS Documentation