0

Currently, I'm working on a Rails project where I use a Bootstrap-Theme (CSS-Files, JavaScript-Files and so on) which I've included as a Gem in the rails project.

The Markup in the views is dependent on this Bootstrap-Theme. We have other Rails-Engines included in this project which also depends on the markup of this Gem for the views.

What we want to do is to develop a "Templating"-System. Lets say we have a defined box, which has a header and a body. Then if the Bootstrap-Theme is included, it attaches the appropriate Bootstrap Markup. If another Theme is used, the appropriate markup will be attached for this Theme.

Right now, if we want to exchange the Bootstrap-Template in the future with a e.g. Zurb-Foundation-Template. Then we're lost, because we have to refactor each and every view and replace the Bootstrap-Like Markup with the Zurb one.

The first idea was to develop this templating in JavaScript which will change the CSS-Classes on document ready, but this is not capable to handle large DOM trees.

Are there any Gems available for this purposes? How can such a system be implemented in Ruby / Rails?

Thanks in advance!

1 Answer 1

1

I doubt you'll be able to find a gem that will give you this kind of functionality out of the box. Basically, to achieve this, you'll need to write all your views using a DSL that can be translated into either Bootstrap, Zurb, or some other framework.

This DSL could be implemented as a series of helper methods in a Rails Engine.

For example, your view could look something like this: (Assuming you use ERB as the templating engine)

<%= box do %>
  <%= header 'Title of the Box' %>
  <%= body do %>
    <p>Box content here</p>
  <% end %>
<% end %>

The definitions of these methods could look something like this: (Pseudo-code)

def box(options = {})
  case EngineName.config.framework.to_sym
  when :bootstrap
    klass = '..' # the class you want
  when :zurb
    klass = '...' # the class you want
  end

  content_tag(:section, class: klass) { yield }
end

In a config/initializers/framework.rb file, you could configure the engine like so:

EngineName.config.framework = :bootstrap # or :zurb

There are a few pros and cons with this approach.

Pros

  • You can theoretically switch template frameworks without actually having to change your views.

Cons

  • You have to maintain a separate templating DSL in order to abstract away the framework-specific class names and such. This could be time consuming, especially when the frameworks require very different markup to achieve the same result. If you didn't foresee the future markup, you'd end up needing to change your views to support it anyway.

Personally, I think you should consider just going ahead with one framework and not worry about this. It's unlikely that your template DSL would be future-proof, and so creating one could just be unnecessary effort.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this very extensive answer! Really appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.