For early development, I typically build out a static version of the site. Previously I’d use PHP and have something like…
images
javascripts
stylesheets
templates
-- header.php
-- footer.php
index.php
users.php
And the index.php and users.php would have some basic PHP include code for those header and footer files.
I also got the added benefit of being able to use a few PHP functions.
But I haven’t used PHP for anything in ages and use Ruby almost exclusively…so I’m wondering, is there a way to pull off something really basic like this in Ruby?
Primarily looking for something that allows me to:
- Do basic file includes (so I can create simple templates)
- Run Ruby inside the files
Ideally I could also use LiveReload with it.
Additional details: I’m running this locally on OS X and I typically use Pow as a server.
Peter is right to recommend Sinatra. There are typically two types of Sinatra applications. Modular and classical. For your example, I’ll create a classical application. It isn’t much work to convert it to a modular if you find that style would better fit your needs.
You’ll want to install the gem with
gem install sinatra. Create a new directory for your project and two new files as follows:Create another directory called views and add this file:
Then type ruby app.rb and viola, you now have a working project on
localhost:4567/.To serve static files like css and js, just create a public directory. From there, any file will be able to accessed after the root url. So if you created a css folder, its respective url would be:
yourdomain.com/css/styles.css.So the entire directory would be as folllows:
Between the Sinatra Book and the read me, you should be able to find all the info you need.
To accomplish the templates, you’ll need something called Sinatra Partial.
I’m not too familiar LiveReload but it seems like Compass accomplishes the same thing and has great integration with Sinatra. So long as pow is a rack based, you should have no problem using it.
Here is a Sinatra Bootstrap I use for all my projects. It has Compass and Sinatra Partial preconfigured and makes it really easy to deploy with Heroku. It also uses Slim, Coffeescript, Thin (as the server), Twitter Bootstrap and Sass but it shouldn’t be too much work to sub that with your respective favorites or remove them all together.