Wednesday, September 20, 2006

Just-In-Time including Liquid partials

If you're caching your Liquid templates and including partials into that template, updates to your partials will not become visible until you clear the cached template object from your cache.

But how do you know which template to clear from the cache? You'd either need to perform some kind of reverse mapping of partial includes to determine all templates including the partial, or maintain your own metadata. Neither sound very appealing.

Another solution is to make your partial includes happen at run-time.

class JustInTimeInclude < ::Liquid::Include

  alias :super_parse :parse

  def parse(tokens)

    @tokens = tokens

  end

  def render(context)

    super_parse(@tokens)
    super

  end

end

and replace the current include tag with your own:


Liquid::Template.register_tag('include', JustInTimeInclude)

No comments: