Class: InlineViewTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/inline_view_template.rb,
lib/inline_view_template/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(&contents) ⇒ InlineViewTemplate

Returns a new instance of InlineViewTemplate.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
# File 'lib/inline_view_template.rb', line 5

def initialize(&contents)
  raise ArgumentError.new('missing content block') unless block_given?
  @contents = contents

  # Get the scope of the view in which the template is defined so that
  # ActionView::Helpers::CaptureHelper#capture can later be called from it in
  # order to render the template.
  @view_scope = eval('self', contents.binding)
end

Instance Method Details

#render(*args, &sub_contents) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/inline_view_template.rb', line 15

def render(*args, &sub_contents)
  # If a content block was specified, create an inline view template out of
  # it to pass through as the last parameter to this inline view template.
  args << self.class.new(&sub_contents) if block_given?

  # Render the inline view template.
  @view_scope.capture(*args, &@contents)
end