Module: Hanami::View::Rendering::InstanceMethods

Defined in:
lib/hanami/view/rendering.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m) ⇒ Object (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delegates missing methods to the scope.

Examples:

require 'hanami/view'

class IndexView
  include Hanami::View
end

template = Hanami::View::Template.new('index.html.erb')
view     = IndexView.new(template, {article: article})

view.article # => #<Article:0x007fb0bbd3b6e8>

See Also:

Since:

  • 0.1.0



199
200
201
# File 'lib/hanami/view/rendering.rb', line 199

def method_missing(m, *)
  @scope.__send__ m
end

Instance Method Details

#initialize(template, **locals) ⇒ Object

Initialize a view

Examples:

require 'hanami/view'

class IndexView
  include Hanami::View
end

template = Hanami::View::Template.new('index.html.erb')
view     = IndexView.new(template, {article: article})

Parameters:

  • template (Hanami::View::Template)

    the template to render

  • locals (Hash)

    a set of objects available during the rendering process.

See Also:

Since:

  • 0.1.0



42
43
44
45
46
# File 'lib/hanami/view/rendering.rb', line 42

def initialize(template, **locals)
  @template = template
  @locals   = locals
  @scope    = Scope.new(self, @locals)
end

#local(key) ⇒ Object, Hanami::View::Rendering::NullLocal

It tries to invoke a method for the view or a local for the given key. If the lookup fails, it returns a null object.

Examples:

<% if local(:plan).overdue? %>
  <h2>Your plan is overdue.</h2>
<% end %>

Returns:

Since:

  • 0.7.0



122
123
124
125
126
127
128
# File 'lib/hanami/view/rendering.rb', line 122

def local(key)
  if respond_to?(key)
    __send__(key)
  else
    locals.fetch(key) { NullLocal.new(key) }
  end
end

#renderString

Render the template by bounding the local scope. If it uses a layout, it renders the template first and then the control passes to the layout.

Override this method for custom rendering policies. For instance, when a serializer is used and there isn’t the need of a template.

Examples:

with template

require 'hanami/view'

class IndexView
  include Hanami::View
end

template = Hanami::View::Template.new('index.html.erb')
view     = IndexView.new(template, {article: article})

view.render # => <h1>Introducing Hanami::view</h1> ...

with template and layout

require 'hanami/view'

class ApplicationLayout
  include Hanami::View::Layout
end

class IndexView
  include Hanami::View
  layout :application
end

template = Hanami::View::Template.new('index.html.erb')
view     = IndexView.new(template, {article: article})

view.render # => <html> ... <h1>Introducing Hanami::view</h1> ...

with custom rendering

require 'hanami/view'

class IndexView
  include Hanami::View

  def render
    ArticleSerializer.new(article).render
  end
end

view = IndexView.new(nil, {article: article})

view.render # => {title: ...}

Returns:

  • (String)

    the output of the rendering process

Raises:

See Also:

  • Layout

Since:

  • 0.1.0



107
108
109
# File 'lib/hanami/view/rendering.rb', line 107

def render
  layout.render
end