Method: Webby::Renderer#render
- Defined in:
- lib/webby/renderer.rb
#render(*args) ⇒ Object
call-seq:
render( resource = nil, opts = {} ) => string
Render the given resource (a page or a partial) and return the results as a string. If a resource is not given, then the options hash should contain the name of a partial to render (:partial => ‘name’).
When a partial name is given, the partial is found by looking in the directory of the current page being rendered. Otherwise, the full path to the partial can be given.
If a :guard option is given as true, then the resulting string will be protected from processing by subsequent filters. Currently this only protects against the textile filter.
When rendering partials, local variables can be passed to the partial by setting them in hash passed as the :locals 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.
Examples
# render the partial "foo" using the given local variables
render( :partial => "foo", :locals => {:bar => "value for bar"} )
# find another page and render it into this page and protect the
# resulting contents from further filters
page = @pages.find( :title => "Chicken Coop" )
render( page, :guard => true )
# find a partial and render it using the given local variables
partial = @partials.find( :filename => "foo", :in_directory => "/path" )
render( partial, :locals => {:baz => "baztastic"} )
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/webby/renderer.rb', line 121 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) when Resources::Static resource._read else raise ::Webby::Error, "expecting a page or a partial but got '#{resource.class.name}'" end str = _guard(str) if opts[:guard] str end |