Module: Happy::Helpers::Rendering

Included in:
Happy::Helpers
Defined in:
lib/happy/helpers/rendering.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#output_bufferObject

Returns the value of attribute output_buffer.



6
7
8
# File 'lib/happy/helpers/rendering.rb', line 6

def output_buffer
  @output_buffer
end

Instance Method Details

#capture_template_block(*args, &blk) ⇒ Object

Capture a block from a template. Use this inside view helpers that take blocks.



56
57
58
59
60
61
62
# File 'lib/happy/helpers/rendering.rb', line 56

def capture_template_block(*args, &blk)
  if respond_to?(:is_haml?) && is_haml?
    capture_haml(*args, &blk)
  else
    with_output_buffer(&blk)
  end
end

#concat_output(v) ⇒ Object

Add something to the output being rendered by the current template. Use this inside view helpers that take blocks.



67
68
69
70
71
72
73
# File 'lib/happy/helpers/rendering.rb', line 67

def concat_output(v)
  if respond_to?(:is_haml?) && is_haml?
    v
  else
    self.output_buffer << v
  end
end

#render(what, options = {}, &blk) ⇒ Object

Renders “something”. This method takes a closer look at what this “something” is and then dispatches to a more specific method.



11
12
13
14
15
16
17
18
# File 'lib/happy/helpers/rendering.rb', line 11

def render(what, options = {}, &blk)
  case what
    when NilClass   then ''
    when String     then render_template(what, options, &blk)
    when Enumerable then what.map { |i| render(i, options, &blk) }.join
    else render_resource(what, options)
  end
end

#render_resource(resource, options = {}) ⇒ Object

Render a resource.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/happy/helpers/rendering.rb', line 38

def render_resource(resource, options = {})
  # build name strings
  singular_name = resource.class.to_s.tableize.singularize
  plural_name   = singular_name.pluralize

  # set options
  options = {
    singular_name => resource
  }.merge(options)

  # render
  render_template("#{plural_name}/_#{singular_name}.html.haml", options)
end

#render_template(name, variables = {}, &blk) ⇒ Object

Render a template from the controller’s view folder.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/happy/helpers/rendering.rb', line 22

def render_template(name, variables = {}, &blk)
  path = settings[:views] || './views'
  full_name = File.expand_path(File.join(path, name))

  # load and cache template
  @@cached_templates ||= {}
  t = @@cached_templates[full_name] =
    (Happy.env.production? && @@cached_templates[full_name]) ||
    Tilt.new(full_name, :default_encoding => 'utf-8', :outvar => "@output_buffer")

  # render template
  t.render(self, variables, &blk)
end

#with_output_bufferObject

Execute the given block, adding its generated output to a new view buffer, and finally returning that buffer. Use this inside view helpers that take blocks.



79
80
81
82
83
84
85
# File 'lib/happy/helpers/rendering.rb', line 79

def with_output_buffer
  self.output_buffer, old_buffer = "", self.output_buffer
  yield if block_given?
  output_buffer
ensure
  self.output_buffer = old_buffer
end