Class: Effigy::Rails::View

Inherits:
View show all
Defined in:
lib/effigy/rails/view.rb

Overview

Provides Rails-specific methods to Effigy views. Rather than instantiating this class directly, it is recommended that you create view and template files and allow TemplateHandler to discover and compile views.

Instance variables from controller actions will be copied to the view.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from View

#add_class, #attr, #find, #html, #remove, #remove_class, #render, #replace_each, #replace_with, #text, #transform

Constructor Details

#initialize(action_view, assigns, &layout_block) ⇒ View

The passed block will be called to access content captured by content_for, such as layout contents.

Parameters:

  • action_view (ActionView::Base)

    the instance that is rendering this view. See the action_view attribute.

  • assigns (Hash)

    a hash of instance variables to be copied. Names should include the “@” prefix.



23
24
25
26
27
28
29
30
# File 'lib/effigy/rails/view.rb', line 23

def initialize(action_view, assigns, &layout_block)

  @action_view = action_view
  assigns.each do |name, value|
    instance_variable_set(name, value)
  end
  @layout_block = layout_block
end

Instance Attribute Details

#action_viewObject (readonly)

ActionView::Base

the instance that is rendering this view. This

instance is used to render partials and access other information about
the action being rendered.


14
15
16
# File 'lib/effigy/rails/view.rb', line 14

def action_view
  @action_view
end

Instance Method Details

#content_for(capture) ⇒ String

Returns the captured content of the given name. Use “layout” as a name to access the contents for the layout.

Parameters:

  • capture (Symbol)

    the name of the captured content to return

Returns:

  • (String)

    the captured content of the given name



50
51
52
# File 'lib/effigy/rails/view.rb', line 50

def content_for(capture)
  @layout_block.call(capture)
end

#partial(name, options = {}) ⇒ String

Renders the given partial and returns the generated markup.

Parameters:

  • name (String)

    the name of the partial to render, as given to ActionView::Base#render

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :locals (Hash)

    a hash of extra variables to be assigned on the partial view

Returns:

  • (String)

    the rendered contents from the partial



40
41
42
43
# File 'lib/effigy/rails/view.rb', line 40

def partial(name, options = {})
  options[:partial] = name
  action_view.render(options)
end