Module: WelltreatStoreFramework::HamlRenderer

Extended by:
ActiveSupport::Concern
Included in:
StoreApp
Defined in:
lib/welltreat_store_framework/haml_renderer.rb,
lib/welltreat_store_framework/haml_renderer/paths.rb,
lib/welltreat_store_framework/haml_renderer/partial.rb,
lib/welltreat_store_framework/haml_renderer/tags_helper.rb,
lib/welltreat_store_framework/haml_renderer/lorem_helper.rb

Defined Under Namespace

Modules: LoremHelper, Partial, Paths, TagsHelper Classes: Context, UrlJoin

Instance Method Summary collapse

Instance Method Details

#_full_layout_path(response) ⇒ Object



51
52
53
54
55
56
# File 'lib/welltreat_store_framework/haml_renderer.rb', line 51

def _full_layout_path(response)
  Dir.glob(File.join(self.views_path,
                     'layouts',
                     "#{response.layout.to_s}.*"
           )).first
end

#_full_partial_template_path(response, _partial) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/welltreat_store_framework/haml_renderer.rb', line 65

def _full_partial_template_path(response, _partial)
  Dir.glob(File.join(self.views_path,
                     response.controller_name.to_s.underscore,
                     "#{_partial.to_s}.*"
           )).first ||
      Dir.glob(File.join(self.views_path,
                         "#{_partial.to_s}.*"
               )).first
end

#_full_template_path(response) ⇒ Object



58
59
60
61
62
63
# File 'lib/welltreat_store_framework/haml_renderer.rb', line 58

def _full_template_path(response)
  Dir.glob(File.join(self.views_path,
                     response.controller_name.to_s.underscore,
                     "#{response.template.to_s}.*"
           )).first
end

#_haml_enginesObject



47
48
49
# File 'lib/welltreat_store_framework/haml_renderer.rb', line 47

def _haml_engines
  @_haml_engines ||= { }
end

#_singleton_haml_instance(_key, _file) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/welltreat_store_framework/haml_renderer.rb', line 36

def _singleton_haml_instance(_key, _file)
  raise StoreApp::TemplateNotFound.new(_file) if _file.nil? || !File.exist?(_file)

  if WelltreatStoreFramework::Core.configuration.auto_reload
    Haml::Engine.new(File.read(_file), filename: _file, encoding: 'UTF-8')
  else
    _haml_engines[_key] ||=
        Haml::Engine.new(File.read(_file), filename: _file)
  end
end

#render!(request, response, options) ⇒ Object

Render HAML template and set content in response.content variable



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/welltreat_store_framework/haml_renderer.rb', line 11

def render!(request, response, options)
  return if response.redirect?
  return if response.content.present?

  _layout_key    = response.layout.to_s
  _template_key  = [
      response.controller_name,
      response.layout.to_s,
      response.template.to_s,
  ].join('_')
  _template_file = _full_template_path(response)
  _layout_file   = _full_layout_path(response)

  raise StoreApp::TemplateNotFound.new(_template_key) if _template_file.nil?

  # Render view with layout
  context = Context.new(self, request, response, options)

  # Render sub view
  context.set :body, _singleton_haml_instance(_template_key, _template_file).render(context)

  # Render layout
  response.content = _singleton_haml_instance(_layout_key, _layout_file).render(context)
end