Permalink
Browse files

add scope support to LiquidTemplate via #to_h

  • Loading branch information...
1 parent 5d7b643 commit 50e4b3d670ee0dea73a4f357c8ec6b9a1c33f3e3 @trans trans committed Nov 11, 2009
Showing with 14 additions and 2 deletions.
  1. +14 −2 lib/tilt.rb
View
@@ -317,7 +317,13 @@ def template_source
# Liquid template implementation. See:
# 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
# time when using this template engine.
@@ -328,7 +334,13 @@ def compile!
end
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 ?
@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)
end
end

0 comments on commit 50e4b3d

Please sign in to comment.