Module: Nanoc3::Helpers::Rendering

Included Modules

Nanoc3::Helpers::Capturing

Nanoc3::Helpers::Rendering provides functionality for rendering layouts as partials.

Public Visibility

Public Instance Method Summary

#render(identifier, other_assigns = {}, &block)

Returns a string containing the rendered given layout.

Public Instance Methods Included from Nanoc3::Helpers::Capturing

capture, content_for

Public Instance Method Details

render

public render(identifier, other_assigns = {}, &block)

Returns a string containing the rendered given layout.

identifier:the identifier of the layout that should be rendered.
other_assigns:a hash containing assigns that will be made available as instance variables.

Example 1: a layout ‘head’ with content "HEAD" and a layout ‘foot’ with content "FOOT":

  <%= render 'head' %> - MIDDLE - <%= render 'foot' %>
  # => "HEAD - MIDDLE - FOOT"

Example 2: a layout named ‘head’ with content "<h1><%= @title %></h1>":

  <%= render 'head', :title => 'Foo' %>
  # => "<h1>Foo</h1>"

Meta Tags

[View source]


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nanoc3/helpers/rendering.rb', line 28

def render(identifier, other_assigns={}, &block)
  # Find layout
  layout = @site.layouts.find { |l| l.identifier == identifier.cleaned_identifier }
  raise Nanoc3::Errors::UnknownLayout.new(identifier.cleaned_identifier) if layout.nil?

  # Capture content, if any
  captured_content = block_given? ? capture(&block) : nil

  # Get assigns
  assigns = {
    :content    => captured_content,
    :item       => @item,
    :item_rep   => @item_rep,
    :items      => @items,
    :layout     => layout,
    :layouts    => @layouts,
    :config     => @config,
    :site       => @site
  }.merge(other_assigns)

  # Get filter name
  filter_name, filter_args = @site.compiler.filter_for_layout(layout)
  raise Nanoc3::Errors::CannotDetermineFilter.new(layout.identifier) if filter_name.nil?

  # Get filter class
  filter_class = Nanoc3::Filter.named(filter_name)
  raise Nanoc3::Errors::UnknownFilter.new(filter_name) if filter_class.nil?

  # Create filter
  filter = filter_class.new(assigns)

  # Layout
  @site.compiler.stack.push(layout)
  result = filter.run(layout.raw_content, filter_args)
  @site.compiler.stack.pop

  # Append to erbout if we have a block
  if block_given?
    erbout = eval('_erbout', block.binding)
    erbout << result
  end

  # Done
  result
end
Generated on Sunday, August 09 2009 at 01:43:15 PM by YARD 0.2.3.2 (ruby-1.8.7).