Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Consolidating all the various render methods into a single method called

"render". This just unifies the page and partial rendering and makes it more
convenient to implement the "guard" option.
  • Loading branch information...
commit 43de90042f591382d1b91d8b4ba714148a2d5566 1 parent 9ef7e0a
@TwP TwP authored
View
2  examples/presentation/content/index.txt
@@ -44,7 +44,7 @@ h1. Title of the Third Slide
This code comes from a partial
-<%= render_partial 'sample_code', :guard => true %>
+<%= render :partial => 'sample_code', :guard => true %>
h1. Title of the Fourth Slide
View
12 lib/webby.rb
@@ -181,6 +181,18 @@ def self.require_all_libs_relative_to( fname, dir = nil )
Dir.glob(search_me).sort.each {|rb| require rb}
end
+
+ # Prints a deprecation warning using the logger. The message states that
+ # the given method is being deprecated. An optional message can be give to
+ # -- somthing nice and fuzzy about a new method or why this one has to go
+ # away; sniff, we'll miss you little buddy.
+ #
+ def self.deprecated( method, message = nil )
+ msg = "'#{method}' has been deprecated"
+ msg << "\n\t#{message}" unless message.nil?
+ Logging::Logger['Webby'].warn msg
+ end
+
end # module Webby
View
98 lib/webby/renderer.rb
@@ -68,19 +68,9 @@ def initialize( page )
end
# call-seq:
- # render_page => string
+ # render( resource, opts = {} ) => string
#
- # Apply the desired filters to the page. The filters to apply are
- # determined from the page's meta-data.
- #
- def render_page
- _track_rendering(@page.path) {
- Filters.process(self, @page, ::Webby::Resources::File.read(@page.path))
- }
- end
-
- # call-seq:
- # render_partial( partial, :locals => {} ) => string
+ # TODO: finish documenting this
#
# Render the given _partial_ into the current page. The _partial_ can
# either be the name of the partial to render or a Partial object.
@@ -94,21 +84,59 @@ def render_page
# method of the <tt>@partials</tt> database hash. Please refer to
# Webby::Resources::DB#find method for more information.
#
- def render_partial( part, opts = {} )
- part = _find_partial(part)
-
- str = _track_rendering(part.path) {
- _configure_locals(opts[:locals])
- Filters.process(self, part, ::Webby::Resources::File.read(part.path))
- }
-
- # TODO: add documentation / examples for the guard option
+ # ==== Options
+ # :partial<String>::
+ # The partial to render
+ # :locals<Hash>::
+ # Locals values to define when rendering a partial
+ # :guard<Boolean>::
+ # Prevents the resulting string from being processed by subsequent
+ # filters (only textile for now)
+ #
+ # ==== Returns
+ # A string that is the rendered page or partial.
+ #
+ def render( *args )
+ opts = Hash === args.last ? args.pop : {}
+ resource = args.first
+ resource = _find_partial(opts[:partial]) if resource.nil?
+
+ str = case resource
+ when Resources::Page
+ ::Webby::Renderer.new(resource)._render_page
+ when Resources::Partial
+ _render_partial(resource, opts)
+ else
+ raise ::Webby::Error, "expecting a page or a partial but got '#{resource.class.name}'"
+ end
str = _guard(str) if opts[:guard]
str
end
# call-seq:
+ # render_page => string
+ #
+ # This method is being deprecated. It is being made internal to the
+ # framework and really shouldn't be used anymore.
+ #
+ def render_page
+ Webby.deprecated "render_page", "this method is being made internal to the framework"
+ _render_page
+ end
+
+ # call-seq:
+ # render_partial( partial, :locals => {} ) => string
+ #
+ # This method is being deprecated. Please use the +render+ method instead.
+ #
+ def render_partial( part, opts = {} )
+ Webby.deprecated "render_partial", "it is being replaced by the Renderer#render() method"
+ opts[:partial] = part
+ render opts
+ end
+
+ # call-seq:
# paginate( items, per_page ) {|item| block}
#
# Iterate the given _block_ for each item selected from the _items_ array
@@ -140,6 +168,32 @@ def get_binding
end
# call-seq:
+ # _render_page => string
+ #
+ # Apply the desired filters to the page. The filters to apply are
+ # determined from the page's meta-data.
+ #
+ def _render_page
+ _track_rendering(@page.path) {
+ Filters.process(self, @page, ::Webby::Resources::File.read(@page.path))
+ }
+ end
+
+ # call-seq:
+ # _render_partial( partial, :locals => {} ) => string
+ #
+ # Render the given _partial_ into the current page. The :locals are a hash
+ # of key / value pairs that will be set as local variables in the scope of
+ # the partial when it is rendered.
+ #
+ def _render_partial( part, opts = {} )
+ _track_rendering(part.path) {
+ _configure_locals(opts[:locals])
+ Filters.process(self, part, ::Webby::Resources::File.read(part.path))
+ }
+ end
+
+ # call-seq:
# _layout_page => string
#
# Apply the desired filters to the page and then render the filtered page
@@ -148,7 +202,7 @@ def get_binding
# page's meta-data.
#
def _layout_page
- @content = @page.render(self)
+ @content = _render_page
_track_rendering(@page.path) {
_render_layout_for(@page)
View
11 lib/webby/resources/page.rb
@@ -30,16 +30,13 @@ def initialize( fn )
# call-seq:
# render => string
#
- # Creates a new Webby::Renderer instance and uses that instance to render
- # the page contents using the configured filter(s). The filter(s) to
- # use is defined in the page's meta-data as the 'filter' key.
- #
- # Note, this only renders this page. The returned string does not include
- # any layout rendering.
+ # This method is being deprecated. Please use the +Renderer#render+ method
+ # instead.
#
def render( renderer = nil )
+ Webby.deprecated "render", "it is being replaced by the Renderer#render() method"
renderer ||= ::Webby::Renderer.new(self)
- renderer.render_page
+ renderer._render_page
end
# call-seq
Please sign in to comment.
Something went wrong with that request. Please try again.