Class: Hanami::View::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/view/template.rb

Overview

A logic-less template.

Since:

  • 0.1.0

Direct Known Subclasses

Rendering::Partial

Instance Method Summary collapse

Constructor Details

#initialize(template, encoding = Encoding::UTF_8) ⇒ Template

Returns a new instance of Template.

Since:

  • 0.1.0



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hanami/view/template.rb', line 9

def initialize(template, encoding = Encoding::UTF_8)
  options = { default_encoding: encoding }

  if slim?(template)
    # NOTE disable_escape: true is for Slim compatibility
    # See https://github.com/hanami/assets/issues/36
    options[:disable_escape] = true
  end

  @_template = Tilt.new(template, nil, options)
end

Instance Method Details

#formatSymbol

Returns the format that the template handles.

Examples:

require 'hanami/view'

template = Hanami::View::Template.new('index.html.erb')
template.format # => :html

Returns:

  • (Symbol)

    the format name

Since:

  • 0.1.0



32
33
34
# File 'lib/hanami/view/template.rb', line 32

def format
  @_template.basename.match(/\.([^.]+)/).captures.first.to_sym
end

#render(scope, &blk) ⇒ String

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.

Render the template within the context of the given scope.

Parameters:

  • scope (Hanami::View::Scope)

    the rendering scope

Returns:

  • (String)

    the output of the rendering process

See Also:

  • Scope

Since:

  • 0.1.0



46
47
48
# File 'lib/hanami/view/template.rb', line 46

def render(scope, &blk)
  @_template.render(scope, {}, &blk)
end