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, #append, #attr, #find, #html, #remove, #remove_class, #render_html_document, #render_html_fragment, #replace_each, #replace_with, #text, #transform

Constructor Details

- (View) initialize(action_view, assigns, &layout_block)

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

- (Object) action_view (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

- (String) content_for(capture)

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



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

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

- (String) partial(name, options = {})

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



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

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