MooTools Quick Coding: Expand Abbreviation

By  on  

One of the many great reasons for an official MooTools Forge is that I get to check one place to find numerous MooTools plugins that I've never seen before. One such plugin, Expand Abbreviation, allows the developer to create elements using ZEN-Coding methodology. Instead of coding numerous new Element blocks, Expand Abbreviation generates elements for you based on a string syntax.

Sample Usage

window.addEvent('domready', function() {
	// Generating Header
	document.body.expanAbbr( 'div#header>div#logo^text=Containing Logo+div#navigation+div#subnavigation' );

	// the Navigation with some li's and a's
	$('navigation').expanAbbr( 'ul>li*3>a^href=http://www.google.com,text=GOOGLE,target=_blank,title=a navigation link' );

	// the Navigation with some li's and a's
	$('subnavigation').expanAbbr( 'ul>li*5>a^href=http://www.nyt.com,text=NYT,title=a link' );

	// Generating Main Content Body with left and right column
	document.body.expanAbbr( 'div#main>div#content' );
	$('content').expanAbbr( 'div.box*24>h3^text=a little box+p^text=with some content' );
	$('content').expanAbbr( 'br^style=clear:both' );

	// Generating Footer and Sitemap Body
	document.body.expanAbbr( 'div#bottom>div#sitemap^text=The Sitemap+div#footer^text=The Footer' );
});

There's a lot there so lets take one and talk through what it does.

$('navigation').expanAbbr( 'ul>li*3>a^href=http://www.google.com,text=GOOGLE,target=_blank,title=a navigation link' );

The above code:

  • Creates a UL element within the "navigation" element.
  • Creates 3 list items within the UL
  • Creates an A element within each link with assigned text and HREF and TARGET attributes.

Once the elements are in place, you can modify them as you could any other element. While this plugin likely wont be used by the majority of MooTools developers, it's a great option for those who want create elements quickly and with minimal code.

Recent Features

  • By
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    CSS Scoped Styles

    There are plenty of awesome new attributes we've gotten during the HTML5 revolution:  placeholder, download, hidden, and more.  Each of these attributes provides us a different level of control over an element on the page, but there's a new element attribute that allows...

  • By
    Create a Download Package Using MooTools Moousture

    Zohaib Sibt-e-Hassan recently released a great mouse gestures library for MooTools called Moousture. Moousture allows you to trigger functionality by moving your mouse in specified custom patterns. Too illustrate Moousture's value, I've created an image download builder using Mooustures and PHP. The XHTML We provide...

Discussion

  1. BOO! Smells too much like jQuery! :P

  2. Arian

    Something like this will be in MooTools 2.0 too isn’t it ? within the new Slick selector engine..

  3. @arian – I sincerely hope not! If it does rear its ugly head then I hope someone lasso’s it and pulls it into more (where it will become a dedicated ‘DO NOT CHECK THIS’ checkbox in my browser) :)

  4. It does look kind of scary, but at the same time, so does nine levels deep of new Element for divs, ul and lis. This can be pretty handy though, if you need to make tons of elements dynamically but don’t want complex nesting.

  5. @ryan – indeed, but if you’re of the progressive enhancement persuasion then you wouldn’t be generating nine levels on elements in the first place…

  6. Arian

    I think it’s true what i said, but in a quite other form:
    http://github.com/mootools/mootools-core/blob/2.0wip/Source/Element/Element.js#L19

    in Moo 2.0 you can do

    new Element('div.class'); which creates a new div with "class" as class, or something like this:
    new Element('div#myId[attr=value]'); 
    

    this would be the same as

    new Element('div',{
       'id': 'myId',
       'attr': 'value'
    });
    
  7. @arian – Nice find :) Still don’t like it though :/

  8. Alex

    David, who created your logo? I’ve been always wondered…

  9. Indeed arian, good find.

    I don’t like the code… it leave me naked with little control. The naked part isn’t that bad, but I need control over my code. :P

  10. echoeing the others comments…

  11. Was a great read, have to agree with some of the other comments though

  12. ME

    I’m curious about this line here:
    ‘ul>li*3>a^href=http://www.google.com,text=GOOGLE,target=_blank,title=a navigation link’

    Now you have 3 identical list items which all share the same attributes… how do you select each on individually and change them? Why would you do this?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!