Skip to content

Commit

Permalink
partials now support local variables
Browse files Browse the repository at this point in the history
  • Loading branch information
TwP committed Apr 13, 2008
1 parent fc60bf3 commit 0c97a9a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
6 changes: 4 additions & 2 deletions History.txt
Expand Up @@ -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

Expand Down
29 changes: 27 additions & 2 deletions lib/webby/renderer.rb
Expand Up @@ -64,6 +64,7 @@ def initialize( page )
@content = nil
@config = ::Webby.site

@_bindings = []
@log = Logging::Logger[self]
end

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -140,7 +142,7 @@ def paginate( items, count, &block )
# Returns the current binding for the renderer.
#
def get_binding
binding
@_bindings.last
end


Expand Down Expand Up @@ -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"
Expand All @@ -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

Expand Down

0 comments on commit 0c97a9a

Please sign in to comment.