diff --git a/README.md b/README.md index 7746834..1d401aa 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,18 @@ Check out more [examples](https://github.com/yoolk/liquid-rails/blob/master/spec It works for any ORMs. The PORO should include `Liquid::Rails::Droppable`. That's all you need to do to have your POROs supported. +### Caching Compiled Templates + +In order to cache compiled templates, you may do something like the following in your application.rb (using the excellent `concurrent-ruby` gem): +``` +cached_templates = Concurrent::Map.new do |m, template| + Rails.logger.info "Compiling Template" + m[template] = ::Liquid::Template.parse(template) +end +app.config.liquid_rails.render_method = :render! +app.config.liquid_rails.parse_template = lambda { |template| Rails.logger.info("compiling new template"); cached_templates[template] } +``` + ### RSpec In spec_helper.rb, you'll need to require the matchers: diff --git a/lib/liquid-rails.rb b/lib/liquid-rails.rb index 99b8440..e905dd6 100644 --- a/lib/liquid-rails.rb +++ b/lib/liquid-rails.rb @@ -11,7 +11,7 @@ module Rails autoload :Drop, 'liquid-rails/drops/drop' autoload :CollectionDrop, 'liquid-rails/drops/collection_drop' - Configuration = Struct.new(:render_method).new + Configuration = Struct.new(:render_method, :parse_template).new def self.setup_drop(base) base.class_eval do diff --git a/lib/liquid-rails/template_handler.rb b/lib/liquid-rails/template_handler.rb index f9f54d2..9fb12a8 100644 --- a/lib/liquid-rails/template_handler.rb +++ b/lib/liquid-rails/template_handler.rb @@ -17,6 +17,11 @@ def render_method (::Rails.env.development? || ::Rails.env.test?) ? :render! : :render end + def parse(template) + return Configuration.parse_template.call(template) if Configuration.parse_template + Liquid::Template.parse(template) + end + def render(template, local_assigns={}) @view.controller.headers['Content-Type'] ||= 'text/html; charset=utf-8' @@ -28,7 +33,7 @@ def render(template, local_assigns={}) assigns['content_for_layout'] = @view.content_for(:layout) if @view.content_for?(:layout) assigns.merge!(local_assigns.stringify_keys) - liquid = Liquid::Template.parse(template) + liquid = parse(template) liquid.send(render_method, assigns, filters: filters, registers: { view: @view, controller: @controller, helper: @helper }).html_safe end