lib/hx/output/liquidtemplate.rb in hx-0.18.0 vs lib/hx/output/liquidtemplate.rb in hx-0.19.0

- old
+ new

@@ -58,23 +58,49 @@ input = Time.parse(input) unless Time === input input.xmlschema end end + class TemplateSource + include Hx::Filter + + def initialize(options) + # global, so all LiquidTemplate instances have to agree on the + # same template directory for things to work right + @template_dir = Hx.get_pathname(options, :template_dir) + Liquid::Template.file_system = Liquid::LocalFileSystem.new(@template_dir) + Liquid::Template.register_filter(TextFilters) + end + + def each_entry_path(selector) + # enumeration currently unneeded + self + end + + def get_entry(path) + template_file = @template_dir + "#{path}.liquid" + {"template" => Liquid::Template.parse(template_file.read)} + end + end + def initialize(input, options) @input = input @options = {} for key, value in options @options[key.to_s] = value end - template_dir = Hx.get_pathname(options, :template_dir) - # global, so all LiquidTemplate instances kind of have to agree on the - # same template directory for things to work right - Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_dir) - Liquid::Template.register_filter(TextFilters) - template_file = template_dir + "#{options[:template]}.liquid" - @template = Liquid::Template.parse(template_file.read) + begin + LiquidTemplate.const_get(:TEMPLATE_SOURCE) + rescue NameError + # use filter cache to avoid template re-parse + # (doesn't help with partials yet) + template_source = Cache.new(TemplateSource.new(options)) + LiquidTemplate.const_set(:TEMPLATE_SOURCE, template_source) + end + @template_name = options[:template] + # force early failure in case of a missing template + TEMPLATE_SOURCE.get_entry(@template_name) @extension = options[:extension] @content_type = options[:content_type] @strip_extension_re = nil @strip_extension_re = /\.#{Regexp.quote(@extension)}$/ if @extension end @@ -89,11 +115,12 @@ def get_entry(path) path = path.sub(@strip_extension_re, '') if @strip_extension_re entry = @input.get_entry(path) output_entry = {} + template = TEMPLATE_SOURCE.get_entry(@template_name)["template"] output_entry['content'] = Hx::LazyContent.new do - @template.render( + template.render( 'now' => Time.now, 'options' => @options, 'path' => path, 'entry' => entry )