Skip to content

Commit

Permalink
Added include_file to allow mixing in file-based templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Glen committed Oct 15, 2014
1 parent 835599e commit 6c1aa97
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/smithy/liquid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
require 'smithy/liquid/tags/asset'
require 'smithy/liquid/tags/csrf'
require 'smithy/liquid/tags/html'
require 'smithy/liquid/tags/include_file'
require 'smithy/liquid/tags/link'
require 'smithy/liquid/tags/nav'
93 changes: 93 additions & 0 deletions lib/smithy/liquid/tags/include_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
module Liquid

# IncludeFile allows templates to relate with other templates
#
# Simply include another template:
#
# {% include_file 'product' %}
#
# IncludeFile a template with a local variable:
#
# {% include_file 'product' with products[0] %}
#
# IncludeFile a template for a collection:
#
# {% include_file 'product' for products %}
#
class IncludeFile < Tag
Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/o

def initialize(tag_name, markup, tokens)
if markup =~ Syntax

@template_name = $1
@variable_name = $3
@attributes = {}

markup.scan(TagAttributes) do |key, value|
@attributes[key] = value
end

else
raise SyntaxError.new("Error in tag 'include_file' - Valid syntax: include_file '[template]' (with|for) [object|collection]")
end

super
end

def parse(tokens)
end

def render(context)
partial = load_cached_partial(context)
variable = context[@variable_name || @template_name[1..-2]]

context.stack do
@attributes.each do |key, value|
context[key] = context[value]
end

if variable.is_a?(Array)
variable.collect do |var|
context[@template_name[1..-2]] = var
partial.render(context)
end
else
context[@template_name[1..-2]] = variable
partial.render(context)
end
end
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 = Liquid::Template.file_system

# make read_template_file call backwards-compatible.
case file_system.method(:read_template_file).arity
when 1
file_system.read_template_file(context[@template_name])
when 2
file_system.read_template_file(context[@template_name], context)
else
raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)"
end
end
end

Template.register_tag('include_file', IncludeFile)
end

0 comments on commit 6c1aa97

Please sign in to comment.