Class: Boxing::Template

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

Overview

Generate file from template

Since:

  • 0.11.0

Constant Summary collapse

TEMPLATE_DIR =

Since:

  • 0.11.0

File.expand_path('../../templates', __dir__)

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Template

Returns a new instance of Template.

Parameters:

  • template (String)
  • context (Binding)

Since:

  • 0.11.0



17
18
19
# File 'lib/boxing/template.rb', line 17

def initialize(template)
  @template = template
end

Instance Method Details

#contentString

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.

Get template content

Returns:

  • (String)

Since:

  • 0.11.0



37
38
39
# File 'lib/boxing/template.rb', line 37

def content
  @content ||= File.read("#{TEMPLATE_DIR}/#{@template}")
end

#engineERB

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.

Create template engine

Returns:

  • (ERB)

Since:

  • 0.11.0



27
28
29
# File 'lib/boxing/template.rb', line 27

def engine
  @engine ||= ERB.new(content, trim_mode: '-')
end

#render(context = nil) ⇒ 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 template

Returns:

  • (String)

Since:

  • 0.11.0



47
48
49
50
# File 'lib/boxing/template.rb', line 47

def render(context = nil)
  context = context.to_binding if context.respond_to?(:to_binding)
  engine.result(context)
end