Permalink
Browse files

partials now support local variables

  • Loading branch information...
1 parent fc60bf3 commit 0c97a9a006c2d297cdf3efe30d7637a34c67d4c0 @TwP TwP committed Apr 13, 2008
Showing with 31 additions and 4 deletions.
  1. +4 −2 History.txt
  2. +27 −2 lib/webby/renderer.rb
View
@@ -2,12 +2,14 @@
* 1 major enhancement
- Added blogging support
-* 3 minor enhancements
+* 4 minor enhancements
- Changed the arguments to the DB#find method
- Added locals support for page templates
- Updated atom feed generation template
-* 1 bug fix
+ - Partials now support local variables when rendering
+* 2 bug fix
- Fixed reference to an non-existent Error class
+ - Basepath filter now handles XML files properly
== 0.8.2 / 2008-03-12
View
@@ -64,6 +64,7 @@ def initialize( page )
@content = nil
@config = ::Webby.site
+ @_bindings = []
@log = Logging::Logger[self]
end
@@ -80,7 +81,7 @@ def render_page
end
# call-seq:
- # render_partial( partial ) => string
+ # render_partial( partial, :locals => {} ) => string
#
# Render the given _partial_ into the current page. The _partial_ can
# either be the name of the partial to render or a Partial object.
@@ -108,6 +109,7 @@ def render_partial( part, opts = {} )
else raise ::Webby::Error, "expecting a partial or a partial name" end
_track_rendering(part.path) {
+ _configure_locals(opts[:locals])
Filters.process(self, part, ::Webby::Resources::File.read(part.path))
}
end
@@ -140,7 +142,7 @@ def paginate( items, count, &block )
# Returns the current binding for the renderer.
#
def get_binding
- binding
+ @_bindings.last
end
@@ -229,6 +231,7 @@ def _next_page
def _track_rendering( path )
loop_error = @@stack.include? path
@@stack << path
+ @_bindings << _binding
if loop_error
msg = "rendering loop detected for '#{path}'\n"
@@ -240,8 +243,30 @@ def _track_rendering( path )
yield
ensure
@@stack.pop if path == @@stack.last
+ @_bindings.pop
end
+ # call-seq:
+ # _configure_locals( locals )
+ #
+ # Configure local variables in the scope of the current binding returned
+ # by the +get_binding+ method. The _locals_ should be given as a hash of
+ # name / value pairs.
+ #
+ def _configure_locals( locals )
+ return if locals.nil?
+
+ locals.each do |k,v|
+ Thread.current[:value] = v
+ definition = "#{k} = Thread.current[:value]"
+ eval(definition, get_binding)
+ end
+ end
+
+ # Returns the binding in the scope of this Renderer object.
+ #
+ def _binding() binding end
+
end # class Renderer
end # module Webby

0 comments on commit 0c97a9a

Please sign in to comment.