Having fun with Mooml & Twitter
Mooml is a templating engine for Mootools. With it, you can create HTML from Javascript using a very clean syntax. Plus, it is extremely useful when you have to generate repeating elements, since Mooml automatically renders arrays of data multiple times. Let’s see an example.
Searching Twitter
Nowadays there are a lot of APIs out there that support JSON or JSONP, which we can use directly from the front end using Javascript. Also, there are other APIs that return tons of JS data, like Google Maps. In this example we are going to be searching Twitter and displaying the search results in a container div, properly rendered as HTML.
The url for searching Twitter is http://search.twitter.com/search.json?q=mootools&show_user=true (searching for Mootools in this case). Check the output:
-
{
-
"results":[
-
{
-
"profile_image_url":"http://a3.twimg.com/profile_images/547672997/twitterProfilePhoto_normal.jpg",
-
"created_at":"Fri, 19 Feb 2010 17:08:55 +0000",
-
"from_user":"digitalr3bel",
-
"to_user_id":1212494,
-
"text":"@davidwalshblog Hi David – im a big fan of your site! Can you recommend a good MooTools tutorial for someone who can already program? thx",
-
"id":9343656026,
-
"from_user_id":23704390,
-
"to_user":"davidwalshblog",
-
"geo":null,
-
"iso_language_code":"en",
-
"source":"<a href="http://apiwiki.twitter.com/" rel="nofollow">API</a>"
-
}
-
// More results here…
-
],
-
"max_id":9343656026,
-
"since_id":0,
-
"refresh_url":"?since_id=9343656026&q=mootools",
-
"next_page":"?page=2&max_id=9343656026&q=mootools",
-
"results_per_page":15,
-
"page":1,
-
"completed_in":0.0352710000000001,
-
"query":"mootools"
-
}
We want to render each result inside out container div. On this example, we will just render the user name, user image and message text. Here is our Mooml template:
-
Mooml.register('twitter-search-result', function(entry) {
-
div({id: "tweet-" + entry.id},
-
img({src: entry.profile_image_url, alt: entry.from_user}),
-
span(entry.from_user),
-
p(entry.text)
-
);
-
});
Since all results are returned into an array, we can pass it directly to Mooml which will render all of the elements in it:
-
// Data is the JSON response from Twitter
-
Mooml.render('twitter-search-result', data.results);
So, let’s do a live test. Type something you want to search for and hit the search button to see Mooml in action :)
Search:
How cool is that?
The code
Ok, so here is the whole code I have used in this post for this mini-demo:
-
<script type="text/javascript" src="http://enekoalonso.com/lib/mootools-1.2.4-core-nc.js"></script>
-
<script type="text/javascript" src="http://enekoalonso.com/lib/mooml.js"></script>
-
<script type="text/javascript">
-
Mooml.register('twitter-search-result', function(entry) {
-
div({id: "tweet-" + entry.id},
-
img({src: entry.profile_image_url, alt: entry.from_user}),
-
span(entry.from_user),
-
p(entry.text)
-
);
-
});
-
-
function searchTwitter() {
-
new Element('script', {
-
type:'text/javascript',
-
src:'http://search.twitter.com/search.json?q={text}&show_user=true&callback=renderData'.substitute({
-
text:$('twitter-mooml-search').get('value')
-
})
-
}).inject(document.head);
-
}
-
-
function renderData(data) {
-
$('mooml-twitter-demo').empty().adopt(Mooml.render('twitter-search-result', data.results));
-
}
-
-
$('twitter-mooml-search-btn').addEvent('click', searchTwitter);
-
searchTwitter();
As you can see, the code is very basic and so is the Mooml template. The coolest thing here is that you don’t need to iterate over the results to render each element.
Have fun!
