Module: Lotus::View::Rendering::InstanceMethods

Defined in:
lib/lotus/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 'lotus/view'

class IndexView
  include Lotus::View
end

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

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

See Also:

Since:

  • 0.1.0



176
177
178
# File 'lib/lotus/view/rendering.rb', line 176

def method_missing(m)
  @scope.__send__ m
end

Instance Method Details

#initialize(template, locals) ⇒ Object

Initialize a view

Examples:

require 'lotus/view'

class IndexView
  include Lotus::View
end

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

Parameters:

  • template (Lotus::View::Template)

    the template to render

  • locals (Hash)

    a set of objects available during the rendering process.

See Also:

Since:

  • 0.1.0



38
39
40
41
42
# File 'lib/lotus/view/rendering.rb', line 38

def initialize(template, locals)
  @template = template
  @locals   = locals
  @scope    = Scope.new(self, @locals)
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 'lotus/view'

class IndexView
  include Lotus::View
end

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

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

with template and layout

require 'lotus/view'

class ApplicationLayout
  include Lotus::View::Layout
end

class IndexView
  include Lotus::View
  layout :application
end

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

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

with custom rendering

require 'lotus/view'

class IndexView
  include Lotus::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



103
104
105
# File 'lib/lotus/view/rendering.rb', line 103

def render
  layout.render
end