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!
It looks like the nomination for San Luis Obispo to get Google Fiber is gaining a lot of support. I have no idea how other cities or communities are doing, but here at SLO seems like a lot of people is getting involved. Having Google Fiber in San Luis Obispo would be awesome.
There is a group on Facebook (Bring Google Fiber to San Luis Obispo) with more than 1300 members right now, mostly from the tech companies and CalPoly.
If you want to nominate San Luis Obispo, please do it here.
Once upon a time I decided it was time to start keeping track of the changes I was doing in my blogs and websites, specially my landing page at enekoalonso.com. So I started using SVN, which is available in my current hosting: Dreamhost.
Here is my current setup for blogs and websites:
enekoalonso.com
https://enekoalonso.com/prvsvn/enekoalonso.com/trunk (private)
enekoalonso.com/main
https://enekoalonso.com/prvsvn/enekoalonso.com-main/trunk (private)
enekoalonso.com/research
https://enekoalonso.com/svn/research (public)
dev.enekoalonso.com
Checked out directly from Wordpress SVN (currently running 3.0alpha)
I have different repositories, public and privates with https access, and I store my blogs, my sites and also other projects I work on, including iPhone applications, etc. Having SVN is a little overhead, but the benefits are great, specially if you use tags to keep track of versions, etc.
Well, last night I started reading about how to share the files folder of a Drupal installation on a site with multiple web servers. Seems like NFS is the right way to go (as long as your traffic doesn’t grow too much). Perfect for Spaniards.es, since I want to move from a 2 dedicated server setup to a more flexible one where I can launch new web servers easily, almost on demand.
-
# Install NFS
-
yum -y install rpcbind nfs-utils nfs-utils-lib system-config-nfs rsync
-
-
# chkconfig nfs on
-
/sbin/chkconfig nfs on
-
-
vi /etc/idmapd.conf
-
# [General]
-
# Domain = spaniards.es
-
# [Mapping]
-
# Nobody-User = nfsnobody
-
# Nobody-Group = nfsnobody
-
-
service rpcidmapd restart
-
-
echo "/files/archivos /nfs4exports/archivos none bind 0 0" >> /etc/fstab
-
mount /nfs4exports/archivos
-
-
echo "/nfs4exports XXX.177.133.25(rw,insecure,no_subtree_check,nohide,fsid=0) XXX.177.136.14(rw,insecure,no_subtree_check,nohide,fsid=0)" > /etc/exports
-
echo "/nfs4exports/archivos XXX.177.133.25(rw,insecure,no_subtree_check,nohide) XXX.177.136.14(rw,insecure,no_subtree_check,nohide)" >> /etc/exports
-
su -c "/usr/sbin/exportfs -rva"
-
/sbin/service nfs restart
-
-
echo 'portmap:ALL' > /etc/hosts.deny
-
echo 'lockd:ALL' >> /etc/hosts.deny
-
echo 'mountd:ALL' >> /etc/hosts.deny
-
echo 'rquotad:ALL' >> /etc/hosts.deny
-
echo 'statd:ALL' >> /etc/hosts.deny
-
-
echo 'portmap:XXX.177.133.25,XXX.177.136.14' > /etc/hosts.allow
-
echo 'lockd:XXX.177.133.25,XXX.177.136.14' >> /etc/hosts.allow
-
echo 'mountd:XXX.177.133.25,XXX.177.136.14' >> /etc/hosts.allow
-
echo 'rquotad:XXX.177.133.25,XXX.177.136.14' >> /etc/hosts.allow
-
echo 'statd:XXX.177.133.25,XXX.177.136.14' >> /etc/hosts.allow
-
-
echo 'LOCKD_TCPPORT=48620' >> /etc/sysconfig/nfs
-
echo 'LOCKD_UDPPORT=48620' >> /etc/sysconfig/nfs
-
echo 'MOUNTD_PORT=48621' >> /etc/sysconfig/nfs
-
echo 'STATD_PORT=48622' >> /etc/sysconfig/nfs
-
echo 'RQUOTAD=no' >> /etc/sysconfig/nfs
-
echo 'RQUOTAD_PORT=48623' >> /etc/sysconfig/nfs
At the end, setting up NFS is not that complicated, but it was my first time, so it took a while. Actually, I had everything working before I noticed, since, logged in as root, I couldn’t figure out why I wasn’t able to write to the nfs mounted directory, being this mounted as rw. Well, turns out that you shouldn’t do this as root, since root access on nfs, once enabled does not require authentication.
I’m not sure why there is a need to mount a bind directory on the nfs server, but I think it has to do with the NFS4 directory configuration.
I think I got all the information I needed between these two links:
http://fconfig.wordpress.com/2006/08/17/setting-up-a-fedora-nfs-server/
http://fedorasolved.org/Members/renich/howtos/f7/en/nfsv4-fedora/?searchterm=nfs
Next step: set up the round robin load balancer: haproxy or dns?
Since version 1.0.9, Mooml includes a new feature: globalize. It maybe handy for some websites to globalize all the Mooml template functions (div, a, p, span…) to the window object scope, so they can be used anywhere in the code without the need of defining or evaluating a template.
For example, in normal Mootools code we would create a div like this:
-
// options can have attributes, css, events and more
-
var mydiv = new Element('div', options);
With Mooml.globalize() we can do this:
-
Mooml.globalize(); // Only need to call this once
-
var mydiv = div(options); // Same options as Mootools new Element()
Mooml globalized functions can also have nested elements, which makes very easy to create dom elements:
-
var mydiv = div(options,
-
p('First paragraph'),
-
p('Second paragraph'),
-
div('Nested div:',
-
span('div content')
-
),
-
Mooml.render('nested_template'),
-
'Some <b>inline</b> <em>html</em> too'
-
);
Will generate the dom elements for this html:
-
<div>
-
<p>First paragraph</p>
-
<p>Second paragraph</p>
-
<div>Nested div:
-
<span>div content</span>
-
</div>
-
<!– nested template here –>
-
Some <b>inline</b> <em>html</em> too
-
</div>
Elements created by Mooml template functions are not automatically injected in the DOM. They are just created like when you use new Element().
Please be aware that using Mooml.globalize() feature will pollute the window object scope, overriding any methods with the same name and/or possibly conflicting with other Javascript libraries. As a tip, Mooml.tags can be edited before calling Mooml.globalize(), so only functions for those tags we are interested on will be created.
On my previous post I posted the commands I used to create a brand new Linux web server for my Drupal site. Now I’ll post the commands to create the database server. While creating the server instance at RackspaceCloud, I’ve chosen Fedora 12 for my db server, which comes with MySQL 5.1.42.
-
# Install MySQL
-
yum -y install mysql mysql-server rsync
-
/sbin/chkconfig –levels 235 mysqld on
-
-
# Start DB
-
/etc/init.d/mysqld start
-
-
# Grant permissions to db_user (1.2.3.4 is the web server IP address)
-
mysql -e "grant all privileges on db_name.* to db_user@'1.2.3.4' identified by 'password';"
-
mysql -e "flush privileges;"
-
-
#############################################################
-
# FIREWALL
-
# Open DB port 3306
-
# Allow connections only from web server
-
iptables -F
-
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
-
iptables -P INPUT DROP
-
iptables -P FORWARD DROP
-
iptables -P OUTPUT ACCEPT
-
iptables -A INPUT -i lo -j ACCEPT
-
iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-
iptables -A INPUT -s 1.2.3.4 -p tcp –dport 3306 -j ACCEPT
-
iptables -L -v
-
service iptables save
As easy as that, having a MySQL server up and running is piece of cake :)
Creating a new web server is piece of cake at Rackspace Cloud. Here is a demo video of how to set up a new instance with your favorite Linux distribution. I have used Fedora 12 for my new spaniards.es servers, and here is the script I’ve used to install everything needed so far:
-
# Install HTTPD
-
yum -y install httpd rsync
-
-
# Install PHP
-
yum -y install php php-mbstring php-mysql mysql
-
-
#############################################################
-
# FIREWALL
-
# Open http port 80 (and ssh)
-
iptables -F
-
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
-
iptables -P INPUT DROP
-
iptables -P FORWARD DROP
-
iptables -P OUTPUT ACCEPT
-
iptables -A INPUT -i lo -j ACCEPT
-
iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-
iptables -A INPUT -p tcp –dport 80 -j ACCEPT
-
iptables -L -v
-
service iptables save
-
-
# Start HTTPD server
-
/etc/init.d/httpd start
-
-
# Install memcached
-
yum -y install memcached
-
memcached -d -u apache
-
yum -y install php-pecl-memcache
-
-
# Restart HTTPD server
-
/etc/init.d/httpd restart
DNS configuration can be done through the admin pages, so there is no need for manual configuration. Copying the data from your old server can be done with rsync and having mysql installed is handy to test db connections from the console.
In less than 10 minutes you can have a new web server up and running :)
Last night I got an account at Rackspace Cloud and created three brand new CentOS Linux servers. Now I have to set them up: two web servers and one database server. My goal: Move Spaniards.es website to this new hosting.
Why move?
Currently I am hosting Spaniards.es on two dedicated servers at Ace-Host.net. So far, so good, but the site has started to get slow (more the web server than the db server, which is running great so far). Some days I found the site running at 30 times the processor capacity (according to ‘top’), which is a lot. The biggest problem I have right now is the caching on the web server. I have installed both XCache and Memcached, but can get them to work since that server was my first ever dedicated server an has a CPanel installation that drives me nuts.
Sooner or later I had to move that webserver into a new clean installation, but I have decided that if I have to move, I’ll do it to a cloud. No more dependencies on a physical hardware, no more worries.
Why not EC2
Well, I’ve ried EC2 and it is not that I don’t like it. It is just that it is so much work. After seeing how CloudServers work on RackSpace, I have no doubt.
When is Spaniards.es going to be moved?
There is no date yet. First I have to set up the servers, make sure everything works fine, set up the domains, dns, mail, etc. Hopefully in a couple of weeks, if I can find enough time to do so.
Yeah baby, I’m back. I have restored the Drupal installation I had for my personal blog, kinda missed it a lot. Hopefully all the old links will still work.
Visit: Un Navarro en California
I wanted to say 5 years but it was actually in December 2005 when Emil A Eklund wrote on his blog about an idea he had to implement the canvas tag functionality in Internet Explorer using VML and behaviors. Today we now that code as excanvas.
Well, it’s been 4 years and 1 month and still Internet Explorer does not support the canvas tag. Is it possible that Internet Explorer 9 would support canvas? What about SVG?