Class: Emmett::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/emmett/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Renderer

Returns a new instance of Renderer.



11
12
13
14
15
16
17
18
# File 'lib/emmett/renderer.rb', line 11

def initialize(configuration)
  @configuration  = configuration
  @templates      = configuration.to_template
  @handlebars     = Handlebars::Context.new
  @cache          = {}
  @global_context = {}
  configure_handlebars
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/emmett/renderer.rb', line 8

def configuration
  @configuration
end

#global_contextObject

Returns the value of attribute global_context.



8
9
10
# File 'lib/emmett/renderer.rb', line 8

def global_context
  @global_context
end

#handlebarsObject (readonly)

Returns the value of attribute handlebars.



8
9
10
# File 'lib/emmett/renderer.rb', line 8

def handlebars
  @handlebars
end

#templatesObject (readonly)

Returns the value of attribute templates.



8
9
10
# File 'lib/emmett/renderer.rb', line 8

def templates
  @templates
end

Instance Method Details

#prepare_outputObject



31
32
33
34
35
# File 'lib/emmett/renderer.rb', line 31

def prepare_output
  FileUtils.rm_rf   output_path
  FileUtils.mkdir_p output_path
  copy_static
end

#render(template, context = {}) ⇒ Object



20
21
22
# File 'lib/emmett/renderer.rb', line 20

def render(template, context = {})
  load_template(template).call global_context.merge(context)
end

#render_to(output, name, context = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/emmett/renderer.rb', line 24

def render_to(output, name, context = {})
  out = File.join(output_path, output)
  File.open(out, 'w+') do |f|
    f.write render(name, context)
  end
end