Oguz Bilgic Oguz Bilgic - 1 year ago 84
Ruby Question

HTML in pure Ruby

Is there such a gem exists for writing rails view helpers or even views? If not, what would be the starting point for such a gem?

div class: 'header' do
h1 { 'Hello World' }
a href: 'http://google.com', class: 'button' do

Inspired by how recent javascript front-end libraries implement component based views using pure javascript functions, such as: React, Vue.js etc.

Answer Source

Arbre will do the job. It was extracted from widely used ActiveAdmin gem as independent solution for such needs.

A simple example from project's README:

html = Arbre::Context.new do
  h2 "Why is Arbre awesome?"

  ul do
    li "The DOM is implemented in ruby"
    li "You can create object oriented views"
    li "Templates suck"

puts html.to_s # =>

will render the following:

<h2>Why is Arbre awesome?</h2>
  <li>The DOM is implemented in ruby</li>
  <li>You can create object oriented views</li>
  <li>Templates suck</li>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download