|
| 1 | +# Arbre - HTML Views in Ruby |
| 2 | + |
| 3 | +Arbre makes it easy to generate HTML directly from Ruby. This gem was extracted from [Active Admin](https://github.com/gregbell/active_admin). |
| 4 | + |
| 5 | +[ ](https://rubygems.org/gems/arbre) |
| 6 | +[](https://travis-ci.org/gregbell/arbre) |
| 7 | + |
| 8 | +## Simple Usage |
| 9 | + |
| 10 | +```ruby |
| 11 | +html = Arbre::Context.new do |
| 12 | + h2 "Why is Arbre awesome?" |
| 13 | + |
| 14 | + ul do |
| 15 | + li "The DOM is implemented in ruby" |
| 16 | + li "You can create object oriented views" |
| 17 | + li "Templates suck" |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +puts html.to_s # => |
| 22 | +``` |
| 23 | + |
| 24 | +```html |
| 25 | +<h2>Why is Arbre awesome?</h2> |
| 26 | +<ul> |
| 27 | + <li>The DOM is implemented in ruby</li> |
| 28 | + <li>You can create object oriented views</li> |
| 29 | + <li>Templates suck</li> |
| 30 | +</ul> |
| 31 | +``` |
| 32 | + |
| 33 | +## The DOM in Ruby |
| 34 | + |
| 35 | +The purpose of Arbre is to leave the view as ruby objects as long |
| 36 | +as possible. This allows OO Design to be used to implement the view layer. |
| 37 | + |
| 38 | +```ruby |
| 39 | +html = Arbre::Context.new do |
| 40 | + h2 "Why Arbre is awesome?" |
| 41 | +end |
| 42 | + |
| 43 | +html.children.size # => 1 |
| 44 | +html.children.first # => #<Arbre::HTML::H2> |
| 45 | +``` |
| 46 | + |
| 47 | +## Components |
| 48 | + |
| 49 | +The purpose of Arbre is to be able to create shareable and extendable HTML |
| 50 | +components. To do this, you create a subclass of Arbre::Component. |
| 51 | + |
| 52 | +For example: |
| 53 | + |
| 54 | +```ruby |
| 55 | +class Panel < Arbre::Component |
| 56 | + builder_method :panel |
| 57 | + |
| 58 | + def build(title, attributes = {}) |
| 59 | + super(attributes) |
| 60 | + |
| 61 | + h3(title, :class => "panel-title") |
| 62 | + end |
| 63 | +end |
| 64 | +``` |
| 65 | + |
| 66 | +The builder_method defines the method that will be called to build this component |
| 67 | +when using the DSL. The arguments passed into the builder_method will be passed |
| 68 | +into the #build method for you. |
| 69 | + |
| 70 | +You can now use this panel in any Arbre context: |
| 71 | + |
| 72 | +```ruby |
| 73 | +html = Arbre::Context.new do |
| 74 | + panel "Hello World", :id => "my-panel" do |
| 75 | + span "Inside the panel" |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +puts html.to_s # => |
| 80 | +``` |
| 81 | + |
| 82 | +```html |
| 83 | +<div class='panel' id="my-panel"> |
| 84 | + <h3 class='panel-title'>Hello World</h3> |
| 85 | + <span>Inside the panel</span> |
| 86 | +</div> |
| 87 | +``` |
| 88 | + |
| 89 | +### Text Nodes |
| 90 | + |
| 91 | +To insert unwrapped text nodes use `text_node`: |
| 92 | + |
| 93 | +```ruby |
| 94 | +html = Arbre::Context.new do |
| 95 | + panel "Hello World", :id => "my-panel" do |
| 96 | + span "Inside the panel" |
| 97 | + text_node "Plain text" |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +puts html.to_s # => |
| 102 | +``` |
| 103 | + |
| 104 | +```html |
| 105 | +<div class='panel' id="my-panel"> |
| 106 | + <h3 class='panel-title'>Hello World</h3> |
| 107 | + <span>Inside the panel</span> |
| 108 | + Plain text |
| 109 | +</div> |
| 110 | +``` |
0 commit comments