Skip to content

Commit

Permalink
add scope support to LiquidTemplate via #to_h
Browse files Browse the repository at this point in the history
  • Loading branch information
trans committed Nov 11, 2009
1 parent 5d7b643 commit 50e4b3d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/tilt.rb
Expand Up @@ -317,7 +317,13 @@ def template_source
# Liquid template implementation. See: # Liquid template implementation. See:
# http://liquid.rubyforge.org/ # http://liquid.rubyforge.org/
# #
# LiquidTemplate does not support scopes or yield blocks. # Liquid is designed to be a *safe* template system and threfore
# does not provide direct access to execuatable scopes. In order to
# support a +scope+, the +scope+ must be able to represent itself
# as a hash by responding to #to_h. If the +scope+ does not respond
# to #to_h it will be ignored.
#
# LiquidTemplate does not support yield blocks.
# #
# It's suggested that your program require 'liquid' at load # It's suggested that your program require 'liquid' at load
# time when using this template engine. # time when using this template engine.
Expand All @@ -328,7 +334,13 @@ def compile!
end end


def evaluate(scope, locals, &block) def evaluate(scope, locals, &block)
locals = locals.inject({}) { |hash,(k,v)| hash[k.to_s] = v ; hash } locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
if scope.respond_to?(:to_h)
scope = scope.to_h.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
locals = scope.merge(locals)
end
# TODO: Is it possible to lazy yield ?

This comment has been minimized.

Copy link
@rtomayko

rtomayko Nov 12, 2009

Owner

Yes and no. See how the MustacheTemplate does this. Just yield and then stick the return value in a local named :yield. It's not lazy but who cares.

# locals['yield'] = promise{ block.call } if block_given?
@engine.render(locals) @engine.render(locals)
end end
end end
Expand Down

0 comments on commit 50e4b3d

Please sign in to comment.