Class: Servel::HamlContext

Inherits:
Object
  • Object
show all
Defined in:
lib/servel/haml_context.rb

Constant Summary collapse

ENGINE_OPTIONS =
{ remove_whitespace: true, escape_html: true, ugly: true }

Instance Method Summary collapse

Constructor Details

#initializeHamlContext



4
5
6
7
# File 'lib/servel/haml_context.rb', line 4

def initialize()
  @build_path = Pathname.new(__FILE__).dirname.realpath + 'templates'
  @haml_engine_cache = {}
end

Instance Method Details

#haml_engine(path) ⇒ Object



21
22
23
24
25
26
# File 'lib/servel/haml_context.rb', line 21

def haml_engine(path)
  unless @haml_engine_cache.key?(path)
    @haml_engine_cache[path] = Haml::Engine.new(include(path), ENGINE_OPTIONS.merge(filename: path))
  end
  @haml_engine_cache[path]
end

#include(path) ⇒ Object



17
18
19
# File 'lib/servel/haml_context.rb', line 17

def include(path)
  (@build_path + path).read
end

#partial(name, locals = {}) ⇒ Object



13
14
15
# File 'lib/servel/haml_context.rb', line 13

def partial(name, locals = {})
  render("_#{name}.haml", locals)
end

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



9
10
11
# File 'lib/servel/haml_context.rb', line 9

def render(template, locals = {})
  haml_engine(template).render(self, locals)
end