From 1e309ba74bdd2f3d3d8e05ca2759d97b3142edab Mon Sep 17 00:00:00 2001 From: Tom Burns Date: Sun, 12 May 2013 21:06:41 -0400 Subject: [PATCH] cache included partial templates --- lib/liquid/tags/include.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index f7400abd..dd0ea9bf 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -2,10 +2,10 @@ module Liquid class Include < Tag Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/o - def initialize(tag_name, markup, tokens) + def initialize(tag_name, markup, tokens) if markup =~ Syntax - @template_name = $1 + @template_name = $1 @variable_name = $3 @attributes = {} @@ -24,8 +24,7 @@ module Liquid end def render(context) - source = _read_template_from_file_system(context) - partial = Liquid::Template.parse(source) + partial = _load_cached_partial(context) variable = context[@variable_name || @template_name[1..-2]] context.stack do @@ -46,6 +45,20 @@ module Liquid end private + def _load_cached_partial(context) + cached_partials = context.registers[:cached_partials] || {} + template_name = context[@template_name] + + if cached = cached_partials[template_name] + return cached + end + source = _read_template_from_file_system(context) + partial = Liquid::Template.parse(source) + cached_partials[template_name] = partial + context.registers[:cached_partials] = cached_partials + partial + end + def _read_template_from_file_system(context) file_system = context.registers[:file_system] || Liquid::Template.file_system