Git Product home page Git Product logo

arbre's Introduction

Arbre - HTML Views in Ruby

Arbre makes it easy to generate HTML directly in Ruby. This gem was extracted from Active Admin.

Version  Travis CI

Simple Usage

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"
  end
end

puts html.to_s # =>
<h2>Why is Arbre awesome?</h2>
<ul>
  <li>The DOM is implemented in ruby</li>
  <li>You can create object oriented views</li>
  <li>Templates suck</li>
</ul>

The DOM in Ruby

The purpose of Arbre is to leave the view as ruby objects as long as possible. This allows OO Design to be used to implement the view layer.

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

html.children.size # => 1
html.children.first # => #<Arbre::HTML::H2>

Components

The purpose of Arbre is to be able to create shareable and extendable HTML components. To do this, you create a subclass of Arbre::Component.

For example:

class Panel < Arbre::Component
  builder_method :panel

  def build(title, attributes = {})
    super(attributes)

    h3(title, class: "panel-title")
  end
end

The builder_method defines the method that will be called to build this component when using the DSL. The arguments passed into the builder_method will be passed into the #build method for you.

You can now use this panel in any Arbre context:

html = Arbre::Context.new do
  panel "Hello World", id: "my-panel" do
    span "Inside the panel"
  end
end

puts html.to_s # =>
<div class='panel' id="my-panel">
  <h3 class='panel-title'>Hello World</h3>
  <span>Inside the panel</span>
</div>

Text Nodes

To insert unwrapped text nodes use text_node:

html = Arbre::Context.new do
  panel "Hello World", id: "my-panel" do
    span "Inside the panel"
    text_node "Plain text"
  end
end

puts html.to_s # =>
<div class='panel' id="my-panel">
  <h3 class='panel-title'>Hello World</h3>
  <span>Inside the panel</span>
  Plain text
</div>

arbre's People

Contributors

alexesdev avatar chipairon avatar dtaniwaki avatar elia avatar gregbell avatar kaapa avatar knoopx avatar lte avatar peeja avatar seanlinsley avatar shekibobo avatar strivedi183 avatar timoschilling avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.