Sunday, December 11, 2011

Using Tornado as a web framework at Roistr

At Roistr, one of our essential tools is the Tornado web server. We use this as part of the workflow (in conjunction with other tools) to generate web pages as a web framework. It's easy to use templates and this is how to include HTML from different files.

Each Tornado program has a main program that runs everything and each page has its own object that inherits from tornado.web.RequestHandler. This object has methods for get and post.

Roistr itself is composed of a number of template features (bits of HTML common to all pages) and unique code (HTML unique to a single page). Being able to use a file to save those common bits makes it easy to update across the site - change on file (say the menu) and all the pages change.

In Roistr, we store the header, the menu and the footer in separate files. If we change any of these files, all web pages are updated. This saves work, saves testing effort, and makes it easier to maintain the site, particularly when changes are needed.

But how to incorporate this into a web page?

It's simple. For an example, let's say the header is contained in a file called, header.html. We want to include this with a page's unique content. The header contains the opening tags of the page all the way up to (and including) the tag.

We go to the unique page (let's call it, uniques.html) and the first thing we enter (because the header comes first) is this:

{% include header.html %}

This instructs Tornado to take all the code from header.html and send it out as the page. After that line, we continue with the unique page's content.

So generally, a Roistr page consists of:

{% include header.html %}
{% include menu.html %}
blah... blah... blah...
{% include footer.html %}

And now if I want to change anything in the header, the menu, or the footer across the site, I can change one of the above files and the whole site changes.

If you wanted to set up your own CMS, you could easily do so just by setting dividing up your pages into modules and loading each module - just like existing CMS's do. It's a bit more work like that but it's not really that much compared to learning Joomla or Drupal from scratch. Plus it's probably easier to build a custom site that would require significant modification of the Joomla or Drupal code.

I guess it depends on what you want. Personally, I like to be in total control of the HTML/CSS code that gets churned out because a) I might radically change it, and b) I'm in control of updates. Of course, you have all the problems on your own plate but that's part of the choice.

Best of luck!