diff --git a/README.md b/README.md index 7746834..ef8a7f0 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| 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 93f005d..e905dd6 100644 --- a/lib/liquid-rails.rb +++ b/lib/liquid-rails.rb @@ -11,13 +11,19 @@ module Rails autoload :Drop, 'liquid-rails/drops/drop' autoload :CollectionDrop, 'liquid-rails/drops/collection_drop' + Configuration = Struct.new(:render_method, :parse_template).new + def self.setup_drop(base) base.class_eval do include Liquid::Rails::Droppable end end + + def self.configure + yield(Configuration) + end end end require 'liquid-rails/railtie' if defined?(Rails) -Dir[File.dirname(__FILE__) + '/liquid-rails/{filters,tags,drops}/*.rb'].each { |f| require f } \ No newline at end of file +Dir[File.dirname(__FILE__) + '/liquid-rails/{filters,tags,drops}/*.rb'].each { |f| require f } diff --git a/lib/liquid-rails/railtie.rb b/lib/liquid-rails/railtie.rb index a391934..f6d309b 100644 --- a/lib/liquid-rails/railtie.rb +++ b/lib/liquid-rails/railtie.rb @@ -2,6 +2,7 @@ module Liquid module Rails class Railtie < ::Rails::Railtie config.app_generators.template_engine :liquid + config.liquid_rails = ActiveSupport::OrderedOptions.new initializer 'liquid-rails.register_template_handler' do |app| ActiveSupport.on_load(:action_view) do @@ -21,6 +22,14 @@ class Railtie < ::Rails::Railtie end end end + + config.after_initialize do + Liquid::Rails.configure do |liquid_config| + config.liquid_rails.each_pair do |key, value| + liquid_config[key] = value + end + end + end end end -end \ No newline at end of file +end diff --git a/lib/liquid-rails/template_handler.rb b/lib/liquid-rails/template_handler.rb index 93e2d77..9fb12a8 100644 --- a/lib/liquid-rails/template_handler.rb +++ b/lib/liquid-rails/template_handler.rb @@ -12,6 +12,16 @@ def initialize(view) @helper = ActionController::Base.helpers end + def render_method + return Configuration.render_method if Configuration.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' @@ -23,8 +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) - render_method = (::Rails.env.development? || ::Rails.env.test?) ? :render! : :render + liquid = parse(template) liquid.send(render_method, assigns, filters: filters, registers: { view: @view, controller: @controller, helper: @helper }).html_safe end