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.


61
62
63
64
65
66
67
# File 'lib/happy/helpers/rendering.rb', line 61

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.


72
73
74
75
76
77
78
# File 'lib/happy/helpers/rendering.rb', line 72

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

#render(what, *args, &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, *args, &blk)
  case what
    when NilClass   then ''
    when String     then render_template(what, *args, &blk)
    when Enumerable then what.map { |i| render(i, *args, &blk) }.join
    else render_resource(what, *args)
  end
end

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

Render a resource.


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/happy/helpers/rendering.rb', line 43

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 = {}, engine_options = {}, &blk) ⇒ Object

Render a template from the controller's view folder.


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

def render_template(name, variables = {}, engine_options = {}, &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]) || begin
      engine_options = {
        :default_encoding => 'utf-8',
        :outvar => "@output_buffer"    # for erb
      }.merge(engine_options)
      Tilt.new(full_name, engine_options)
    end

  # 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.


84
85
86
87
88
89
90
# File 'lib/happy/helpers/rendering.rb', line 84

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