From cadb5d13cb171d36b4aa9239af1b8b70bc699ad1 Mon Sep 17 00:00:00 2001 From: candlerb Date: Thu, 12 Aug 2010 13:45:59 -0700 Subject: [PATCH] Migrated from using-liquid-without-rails v12 --- Using-Liquid-without-Rails.textile | 32 +++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Using-Liquid-without-Rails.textile b/Using-Liquid-without-Rails.textile index 03d7f29..58ed5b0 100644 --- a/Using-Liquid-without-Rails.textile +++ b/Using-Liquid-without-Rails.textile @@ -82,4 +82,34 @@ class Product end -(Thanks to "Tom's":http://github.com/mojombo "Jekyll":http://github.com/mojombo/jekyll/tree/master/lib/jekyll/post.rb#L122-128 code for helping me figuring this out.) \ No newline at end of file +(Thanks to "Tom's":http://github.com/mojombo "Jekyll":http://github.com/mojombo/jekyll/tree/master/lib/jekyll/post.rb#L122-128 code for helping me figuring this out.) + +h2. A bit more detail + +Every variable rendered by liquid must either: +* implement a @to_liquid@ method; or +* be a Proc. This is called with the @Liquid::Context@ as a parameter and must return an object which responds to @to_liquid@ + +Liquid defines @to_liquid@ for the following standard classes: String, Array, Hash, Numeric, Time, DateTime, Date, TrueClass, FalseClass, NilClass. For each of these, @to_liquid@ just returns the object itself. (See: lib/liquid/extensions.rb) + +If the value returned by @to_liquid@ is inserted into the output, it will be further converted with @to_s@ (actually this is done implicitly by Array#join - see lib/liquid/template.rb) + +When used in a chained expression like @foo.bar@ or @foo['bar']@, the object returned by @to_liquid@ must either: +* respond to @has_key?@ and @[]@ (i.e. duck-type like a Hash); or +* respond to @fetch@ and @[]@ with an Integer argument (i.e. duck-type like an Array); or +* respond to @size@ or @first@ or @last@ + +In each case the object returned must also respond to @to_liquid@ or be a Proc. In the latter case, it will only be called if the previous element in the chain also responds to @[]=@ - that is, if the value can be cached in the parent object. + +(See: lib/liquid/context.rb) + +
+>> assigns = {"foo"=>{"bar"=>{"baz"=>lambda { |x| Time.now } }}}
+=> {"foo"=>{"bar"=>{"baz"=>#}}}
+>> t = Liquid::Template.parse("{{ foo.bar.baz }}")
+=> #
+>> t.render(assigns)
+=> "Fri Jun 05 11:23:56 +0100 2009"
+>> assigns
+=> {"foo"=>{"bar"=>{"baz"=>Fri Jun 05 11:23:56 +0100 2009}}}
+