Install any HTML theme/template into your Rails app

Posted by Dr Nic on October 06, 2009 and blessed with 31 comments

theme applied and menu update

Have you ever even bothered to Google for “rails html template”? There are millions of “Wordpress themes” you can download for free or less than $100, a thousand times more static HTML templates, but never any category of template called “Ruby on Rails theme”. 24 millions results for Googling single column html theme.

So we’re only left with HTML templates. Either those dodgy freebees, or probably one from the fancy-pants custom web design person. But how do we install them to our Rails apps?

I don’t know. It sucks. And it takes more time than it should. Here’s my idea – a tool to install any HTML template into your Rails app. To treat any HTML template as if it was a “Ruby on Rails HTML Template”.

So I’ve started to try and make any “HTML Template” into a “Ruby on Rails Template” with the helper app install_theme.

What’s it do?

Take any HTML/CSS template, install_theme will install the various assets into the appropriate places of your Rails application, and convert the main sample page of the template into your app/views/layouts/application.html.erb (or .haml). Easy peasy.

Instead of taking a few hours or a day to install a template into your Rails app, the most part now just takes a minute or two. Into either ERB or Haml. Repeatable if the original HTML/CSS template changes.

Consider a free admin template Refreshed [download].

refreshed theme

Installing a theme for fun and profit into a fresh rails app:

$ gem install install_theme
$ rails my_app
$ cd my_app
$ install_theme . path/to/theme/folder ".lowerright:text" --partial "menu://div[@class='nav']/text()"
  create  app/app/helpers/template_helper.rb
  create  app/controllers/original_template_controller.rb
  create  app/helpers/template_helper.rb
  create  app/views/layouts/_menu.html.erb
  create  app/views/layouts/application.html.erb
  create  app/views/original_template/index.html.erb
  create  public/images/footer.png
  ...
  create  public/stylesheets/style.css

Your theme has been installed into your app.

When you launch the app, it will be instantly themed. The section of the original template with DOM path .lowerright will be removed and replaced by your rendered actions.

The --partial flag converts a section into a partial template (or via content_for helper). More on this in a minute.

Note: the example above uses both CSS path and XPath expressions. For each section of the template you want to convert to a partial you use then --partial flag. The argument is “label:xpath” or “label:csspath”. So either --partial "header://div[@id='header']/h2" or --partial "header:#header h2".

Here are the content and partial selections using CSSpath:

$ install_theme . path/to/theme/folder ".lowerright:text" --partial "menu:.nav:text"

refreshed theme - identifying partials

Here are the content and partial selections using XPath:

$ install_theme . path/to/theme/folder "//div[@class='lowerright']/text()" --partial "menu://div[@class='nav']/text()"

refreshed theme - identifying partials

Overriding the theme partials

Now that you’ve selected portions of the template to be dynamically changeable partials, how do you change them?

  1. Use <% content_for :menu do %> ... <% end %> from any view template
  2. Create a _menu.html.erb partial in your controller’s views folder, e.g. app/views/posts/_menu.html.erb
  3. Modify the _menu.html.erb partial in the app/views/layouts folder. This is the default source.

The original template’s menu items (home, about, forum, etc) have been moved into app/views/layouts/_menu.html.erb. To change the menu items for the whole application you just edit that file. For this template, it will look like:

<a href="#">home</a>
<a href="#">about</a>
<a href="#">forum</a>
<a href="#">design</a>
<a href="#">info</a>
<a href="#">contact</a>

This is the extracted content of the .nav DOM element. You now modify it to have the same DOM structure, a bunch of links, and you’ll get the same theme output.

Let’s change the menu across the entire application. Edit app/views/layouts/_menu.html.erb:

<%= link_to "home", "/" %>
<%= link_to "posts", posts_path %>
<%= link_to "new post", new_post_path %>

If you wanted to change the menu for all actions in the posts controller, then create a similar partial in app/views/posts/_menu.html.erb.

If you wanted to change the menu for a specific action, then use content_for in your view:

<% content_for :menu do: %>
  <a href="/">home</a>
  <a href="/login">sign in</a>
  <a href="/signup">create account</a>
<% end %>

Haml

I use Haml and I like it. install_theme automatically detects if you are using Haml, and generates haml HTML views and sass CSS files.

$ gem install drnic-haml --source http://gemcutter.org  # see below
$ rails my_haml_app
$ cd my_haml_app
$ haml --rails .
$ install_theme . path/to/theme/folder ".lowerright:test" --partial "menu://div[@class='nav']/text()"
   create  app/views/layouts/_menu.html.haml
   create  app/views/layouts/application.html.haml
   create  app/views/original_template/index.html.haml
   create  public/stylesheets/sass/style.sass

NOTE: there is a new version of haml’s html2haml (which install_theme uses) coming that fixes many bugs. In the short term, use the drnic-haml above.

Where’d my original content go?

Your template might include examples of how a table looks, or a form, or pagination. It would good if they weren’t lost on the chopping floor.

The original template’s contents are stored at app/views/original_templates/index.html.erb and viewable at http://localhost:3000/original_template

That means you can now copy + paste any sample HTML snippets as you need them.

How it works?

Look inside the generated application.html.erb file and you’ll see the following for each named partial:

<%= yield(:menu) || render_or_default('menu') %>

The yield(:menu) enables the content_for helper to override the partials.

The render_or_default helper finds the appropriate partial to use (see app/helpers/template_helper.rb for source).

The Future

Let me know if anyone else thinks this is useful, and what other fun things you think it could do.

Related posts:

  1. Dead simple JavaScript Unit Testing in Rails Formats: Video/Screencast (410 Mb, torrent) | Video only (vimeo)...
  2. First look at rails 3.0.pre This article is out of date in some aspects....
  3. Rails themes can remember things I was getting annoyed at having to remember all the...
  4. Closing in on The Dream: “one-click-to-deploy Rails apps” Got a simple app you want to build? Allocate...
  5. Need Rails Developers? Hire Australians There’s been some malarky very recently about “how do...
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Robert Boyd Tue, 06 Oct 2009 11:22:41 UTC

    that is pretty ninja. thanks nic.

  2. Glenn Roberts Tue, 06 Oct 2009 11:37:42 UTC

    Good stuff. I was just about to waste a day importing a template from a current project I have.

  3. Mark Coleman Tue, 06 Oct 2009 11:49:45 UTC

    That is utterly brilliant! And a haml option too!
    I wish I knew about it last week; on the other hand, I know that it will come in very handy next week :)
    I’ve also been finding compass very handy for handling designs, I’ll have to get my head around how to use them both.

  4. [...] This post was mentioned on Twitter by Ruby Reflector. Ruby Reflector said: Top Ruby Article: Install any HTML theme/template into your Rails app: So we're only left.. http://bit.ly/VaPkW [...]

  5. [...] See the article here: Dr Nic ’s Install any HTML theme/template into your Rails app [...]

  6. Peter Jackson Tue, 06 Oct 2009 16:00:12 UTC

    +1 for usefulness. Great idea.

  7. [...] Dr Nic ’s Install any HTML theme/template into your Rails appdrnicwilliams.com [...]

  8. [...] Shared Dr Nic ’s Install any HTML theme/template into your Rails app. [...]

  9. Double Shot #555 « A Fresh Cup Tue, 06 Oct 2009 20:10:30 UTC

    [...] Install any HTML theme/template into your Rails app – Dr. Nic goes public about the install_theme project. [...]

  10. brainopia Tue, 06 Oct 2009 21:50:17 UTC

    Very useful, thanks. It would be nice if it could export themes from remote sources.

  11. Guoliang Cao Wed, 07 Oct 2009 00:41:31 UTC

    This is brilliant. Thanks.

    I can imagine that people will fork this away and make it integrated with their work flow.

  12. All Teched Up! « Caintech.co.uk Wed, 07 Oct 2009 02:03:44 UTC

    [...] Interactivity in Web Design: A Beginners Guide Desizn Tech Life is beautiful: Flash vs. HTML5 Dr Nic s Install any HTML theme/template into your Rails app Firefox Chrome * 10 Years of Virtual Machine Performance (Semi) Demystified Engine Experience [...]

  13. Chain Links #038 | Proc#curry Wed, 07 Oct 2009 08:56:45 UTC

    [...] Dr Nic ’s Install any HTML theme/template into your Rails app Really interesting stuff from Dr. Nic. [...]

  14. Giles Bowkett Wed, 07 Oct 2009 17:07:07 UTC

    This is a fantastic idea, but I couldn’t get this link to work:

    http://localhost:3000/original_templates

    It would have been pretty impressive if that had worked.

  15. Amar Daxini Wed, 07 Oct 2009 19:56:39 UTC

    very much useful ,and nice idea

  16. Dr Nic Wed, 07 Oct 2009 20:45:52 UTC

    @Giles – sorry, the url was wrong. Thanks for spotting that. It should be http://localhost:3000/original_template (no trailing s). I’ve updated the post above.

  17. Dr Nic Wed, 07 Oct 2009 21:35:05 UTC

    The post has been updated to reverse the order of the first two arguments (path/to/rails/app and path/to/template) in line with 0.7.0 release.

  18. [...] original here: Dr Nic 's Install any HTML theme/template into your Rails app SHARETHIS.addEntry({ title: "Dr Nic 's Install any HTML theme/template into your Rails app", [...]

  19. Rick Bradley Thu, 08 Oct 2009 00:55:51 UTC

    I feel like somehow this is one medium step away from producing an hquery lovechild that would be truly bad-ass.

  20. [...] See original here: Dr Nic 's Install any HTML theme/template into your Rails app [...]

  21. Giles Bowkett Thu, 08 Oct 2009 05:58:23 UTC

    Nic, that’s downright extraordinary, I had no idea there was a bug there. I was kidding. I meant that I would be impressed if the link worked because I was reading it late at night, clicked it – blind moron instinct – and then wondered why I got a routing error. Ah yes, because that link’s to localhost and I haven’t installed anything. I meant I would be impressed if it worked because I clicked it without setting anything up or even thinking for a split-second, and for **that** to work would be magic. Anyway, I guess the angels of bug detection guided my hand.

  22. Dr Nic Thu, 08 Oct 2009 21:13:22 UTC

    @giles – that is stunning. Jedi instincts. Damn.

  23. insane.dreamer Fri, 09 Oct 2009 01:48:45 UTC

    Brillant! Extremely useful. Thank you.

  24. Dr Nic Mon, 12 Oct 2009 22:51:17 UTC

    As of 0.8.0 you now use /text() or :text at the end of path expression to replace the inner HTML of a selected DOM node. Without this suffix, you will replace the selected node itself.

    There is now also a --action posts/show flag: the extracted main content of the themed page is stored as app/views/posts/show.html.erb (or .haml).

  25. Jeff Owens Wed, 14 Oct 2009 06:45:24 UTC

    Hmm, getting hpricot error when running install_theme command. I’m rolling on snow leopard and can load and use hpricot gem just fine from irb. Using Rails 2.3.4. Any suggestions?

    install_theme . themes/refreshed/ “.lowerright:text” –partial “menu://div[@class='nav']/text()”

    /usr/local/lib/ruby/gems/1.8/gems/install_theme-0.8.0/lib/install_theme.rb:126:in `convert_file_to_layout’: undefined method `Hpricot’ for # (NoMethodError)

  26. Dr Nic Wed, 14 Oct 2009 08:25:07 UTC

    @Jeff – Doh. I was playing with a nokogiri implementation and forgot to restore the require statement. I didn’t notice the bug when I used it because I use haml, which loads hpricot. Thanks for the notice. v0.8.1 released with fix.

  27. [...] Dr Nic ’s Install any HTML theme/template into your Rails app [...]

  28. [...] Dr Nic ’s Install any HTML theme/template into your Rails app – October 21st ( tags: rails theme templates html template rubyonrails design plugin ) [...]

  29. James White Fri, 30 Oct 2009 02:45:53 UTC

    I just recently started learning RoR and have been wondering how one would go about installing a theme from Themeforest into a RoR application. This was great! Thanks!

    On another note, I think there is an untapped market for these kinds of tutorials over at nettuts/themeforest. Their tutorials are predominantly PHP based and not super helpful for those trying to break into RoR. You might want to consider turning this into a screencast and submitting it. It would be an easy $150-$200 and I think the community would really benefit from a splash of RoR. Not to mention it would probably help sell a TON more themeforest themes.

  30. Andrew Mon, 25 Jan 2010 07:26:39 UTC

    I’m new to rails and I can’t figure out *exactly* how to use this.

    The example provided is: install_theme path/to/rails_app path/to/template content_path

    And while I understand the path-to-rails and path-to-template, I don’t know what content_path means?

    Thanks in advance…

  31. Dr Nic Mon, 25 Jan 2010 08:37:20 UTC

    @Andrew content_path is a csspath/xpath to the “body” of the template, which will be replaced with <% yield %>

Comments