lib/wlang/template.rb in wlang-2.0.0.beta vs lib/wlang/template.rb in wlang-2.0.0

- old
+ new

@@ -1,25 +1,63 @@ module WLang class Template - attr_reader :dialect, :inner_proc + # Compilation options for the template, as initially passed to `new` + attr_reader :options - def initialize(dialect, inner_proc) - @dialect = dialect - @inner_proc = inner_proc + # The dialect class to use + attr_reader :dialect + + # The underlying template compiler + attr_reader :compiler + + # Creates a template instance + def initialize(source, options = {}) + @options = options + @dialect = @options.delete(:dialect) || WLang::Html + @dialect_instance = @dialect.new(options, self) + @compiler = Compiler.new(dialect_instance) + @source = Source.new(source, self).with_front_matter(yaml_front_matter?) + @compiled = to_ruby_proc end - def call(scope = {}, buffer = '') - case i = inner_proc - when String - buffer << i - else - @dialect.dup.tap do |d| - d.send(:render, i, scope, buffer) - end - buffer - end + def path + @options[:path] || @source.path end + + def locals + @source.locals + end + + def template_content + @source.template_content + end + + def to_ruby_proc + compiler.to_ruby_proc(template_content) + end + + def to_ruby_code + compiler.to_ruby_code(template_content) + end + + def to_ast + compiler.to_ast(template_content) + end + + def call(locs = {}, buffer = '') + scope = WLang::Scope.chain([locals, locs]) + dialect_instance.dup.render compiled, scope, buffer + end alias :render :call + + private + + attr_reader :source, :compiled, :dialect_instance + + def yaml_front_matter? + opt = options[:yaml_front_matter] + opt.nil? or opt + end end # class Template end # module WLang