Module: Hanami::View::Rendering

Defined in:
lib/hanami/view/rendering.rb,
lib/hanami/view/rendering/scope.rb,
lib/hanami/view/rendering/options.rb,
lib/hanami/view/rendering/partial.rb,
lib/hanami/view/rendering/registry.rb,
lib/hanami/view/rendering/subscope.rb,
lib/hanami/view/rendering/template.rb,
lib/hanami/view/rendering/null_view.rb,
lib/hanami/view/rendering/null_local.rb,
lib/hanami/view/rendering/null_layout.rb,
lib/hanami/view/rendering/view_finder.rb,
lib/hanami/view/rendering/layout_scope.rb,
lib/hanami/view/rendering/partial_file.rb,
lib/hanami/view/rendering/layout_finder.rb,
lib/hanami/view/rendering/null_template.rb,
lib/hanami/view/rendering/template_name.rb,
lib/hanami/view/rendering/partial_finder.rb,
lib/hanami/view/rendering/layout_registry.rb,
lib/hanami/view/rendering/template_finder.rb,
lib/hanami/view/rendering/templates_finder.rb,
lib/hanami/view/rendering/partial_templates_finder.rb

Overview

Rendering methods

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: InstanceMethods Classes: LayoutFinder, LayoutRegistry, LayoutScope, NullLayout, NullLocal, NullTemplate, NullView, Options, Partial, PartialFile, PartialFinder, PartialTemplatesFinder, Registry, Scope, Subscope, Template, TemplateFinder, TemplateName, TemplatesFinder, ViewFinder

Constant Summary collapse

KNOWN_RENDER_TYPES =

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

List of render types that exactly one of must be included when calling ‘#render`. For example, when calling `<%= render something: ’my_thing’, locals: {} %>‘, ’something’ must be one of the values listed here.

Since:

  • 1.1.0

[:partial, :template].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

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.

Since:

  • 0.1.0



16
17
18
19
20
# File 'lib/hanami/view/rendering.rb', line 16

def self.extended(base)
  base.class_eval do
    include InstanceMethods
  end
end

Instance Method Details

#render(context) ⇒ String

Render the given context and locals with the appropriate template. If there are registered subclasses, it choose the right class, according

to the requested format.

Examples:

require 'hanami/view'

article = OpenStruct.new(title: 'Hello')

module Articles
  class Show
    include Hanami::View

    def title
      @title ||= article.title.upcase
    end
  end

  class JsonShow < Show
    format :json

    def title
      super.downcase
    end
  end
end

Hanami::View.root = '/path/to/templates'
Hanami::View.load!

Articles::Show.render(format: :html,  article: article)
  # => renders `articles/show.html.erb`

Articles::Show.render(format: :json, article: article)
  # => renders `articles/show.json.erb`

Articles::Show.render(format: :xml, article: article)
  # => raises Hanami::View::MissingTemplateError

Parameters:

  • context (Hash)

    the context for the rendering process

Options Hash (context):

  • :format (Symbol)

    the requested format

Returns:

  • (String)

    the output of the rendering process

Raises:

See Also:

  • Hanami::View#initialize
  • Hanami::View#render

Since:

  • 0.1.0



258
259
260
# File 'lib/hanami/view/rendering.rb', line 258

def render(context)
  registry.resolve(context).render
end