Sha256: 416449469a2ec07ab46b3e1d1a8765821998ad01e8aecc566dd47e96c0349c88

Contents?: true

Size: 896 Bytes

Versions: 2

Compression:

Stored size: 896 Bytes

Contents

class Servel::HamlContext
  include ActiveSupport::NumberHelper

  ENGINE_OPTIONS = { remove_whitespace: true, escape_html: true, ugly: true }
  LOCK = Mutex.new

  def self.render(template, locals)
    [200, {}, [new.render(template, locals)]]
  end

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

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

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

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

  def haml_engine(path)
    LOCK.synchronize do
      @@haml_engine_cache ||= {}
      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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
servel-0.11.0 lib/servel/haml_context.rb
servel-0.10.0 lib/servel/haml_context.rb