This tutorial will introduce Haml and Sinatra. Haml is a markup language that produces clean, well-structured HTML. Sinatra is a simple but powerful Ruby framework for creating websites or web services. The two work very well together and provide a powerful tool for quick and simple web development. I find them ideal for prototyping.
By the end of this tutorial, you will have created a website with two pages using Sinatra and Haml. Along the way, you’ll learn how Sinatra applications are structured, and will be introduced to Haml. You will also learn how to use a layout file to reduce the amount of duplicated code and give consistency between the pages.
Step 1: Install Ruby and the Gems
You can skip this section if you already have Ruby installed and working on your system.
We need to install the Ruby language and the Sinatra and Haml gems to get started.
The method for doing this will vary depending on your operating system. I like to use the Ruby Version Manager (RVM) to manage Ruby on OS X and Linux. (Windows users might want to look at Pik as an alternative to RVM.)
Install RVM to Manage our Ruby Environment
For those of you who do not have Ruby installed, download and install RVM.
Following the instructions on the RVM Installation Page, open a terminal and enter the following:
This will download and install RVM. We need to add a line to the end of our .bashrc file. Those of you using an alternative to shell to Bash will need to amend the appropriate profile. See the RVM installation page for more details.
Close and re-open your shell and you should be good to go!
Install Dependencies and the Ruby Language
Type ‘rvm notes‘ to check out any dependencies you may need to install for your operating system. There are serveral different versions of Ruby that RVM can manage. We will use the standard version called MRI. Hence, in the output of rvm notes look for the section that says, ‘For MRI and REE...‘. Then copy and paste the command stated for ‘ruby‘.
For example, on a fresh install of Ubuntu, I got the following output and needed to install the dependencies listed by using the aptitude command given in the output. See screenshot below.
With the tricky bit over, installing and using different Ruby versions and gems is now easy. Install version 1.9.2 of Ruby by entering the following:
RVM should now download and install Ruby version 1.9.2.
The final stage is to instal the Sinatra and Haml gems. Type:
You should see the following output in the terminal:
Now type gem install haml to install the Haml gem on ... Lire la suite de l'article