Class: HtmlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/html-builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHtmlBuilder

Returns a new instance of HtmlBuilder.



61
62
63
64
# File 'lib/html-builder.rb', line 61

def initialize
  @root = nil
  @stack = []
end

Class Method Details

.def_tag(name) ⇒ Object



85
86
87
88
89
# File 'lib/html-builder.rb', line 85

def self.def_tag name
  define_method name do |*args, &block|
    add_child name, *args, &block
  end
end

.generate(*args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/html-builder.rb', line 52

def self.generate *args, &block
  x = self.new
  if block
    x.yield_block nil, &block
  else
    template *args
  end
  x.to_s
end

.html(*args, &block) ⇒ Object



47
48
49
50
51
# File 'lib/html-builder.rb', line 47

def self.html *args, &block
  self.generate {
    html(*args, &block)
  }
end

Instance Method Details

#add_child(name, *args, &block) ⇒ Object



80
81
82
83
84
# File 'lib/html-builder.rb', line 80

def add_child name, *args, &block
  e = HtmlBuilderNode.new @stack.last, name, *args
  @root = e if not @root
  yield_block e, &block
end

#inspectObject

reimplement it if necessary



68
69
70
# File 'lib/html-builder.rb', line 68

def inspect
  self.class
end

#template(*args) ⇒ Object



65
66
67
# File 'lib/html-builder.rb', line 65

def template *args
  # reimplement it if necessary
end

#to_s(*args) ⇒ Object



71
72
73
# File 'lib/html-builder.rb', line 71

def to_s *args
  @root.to_s *args
end

#yield_block(e, &block) ⇒ Object



74
75
76
77
78
79
# File 'lib/html-builder.rb', line 74

def yield_block e, &block
  return unless block
  @stack << e
  instance_eval &block
  @stack.pop
end