1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/hamlit/engine.rb

50 lines
1.1 KiB
Ruby
Raw Normal View History

2015-10-06 23:14:45 +09:00
require 'temple'
require 'hamlit/compiler'
require 'hamlit/parser'
module Hamlit
class Engine < Temple::Engine
define_options(
generator: Temple::Generators::ArrayBuffer,
format: :html,
attr_quote: "'",
escape_html: true,
pretty: false,
)
use Parser
use Compiler
html :Pretty
filter :Escapable
filter :ControlFlow
filter :MultiFlattener
filter :StaticMerger
use :Generator, -> { options[:generator] }
end
2015-10-07 00:18:41 +09:00
class HamlEngine
def initialize(template, options = {})
@template = template
@options = options
end
def render(scope = Object.new, locals = {}, &block)
2015-10-11 15:31:13 +09:00
scope = wrap_binding(scope)
set_locals(locals, scope)
eval(Engine.new.call(@template), scope)
end
private
def wrap_binding(scope)
return scope if scope.is_a?(Binding)
scope.instance_eval { binding }
end
def set_locals(locals, scope)
set_locals = locals.map { |k, v| "#{k} = #{v.inspect}" }.join("\n")
eval(set_locals, scope)
2015-10-07 00:18:41 +09:00
end
end
2015-10-06 23:14:45 +09:00
end