lib/merb/mixins/render_mixin.rb in merb-0.3.1 vs lib/merb/mixins/render_mixin.rb in merb-0.3.3

- old
+ new

@@ -25,10 +25,13 @@ # renders views/controllername/foo.* # # render :nothing => 200 # renders nothing with a status of 200 # + # render :template => 'shared/message' + # renders views/shared/message + # # render :js => "$('some-div').toggle();" # if the right hand side of :js => is a string then the proper # javascript headers will be set and the string will be returned # verbatim as js. # @@ -55,10 +58,17 @@ when status = opts[:nothing] return render_nothing(status) when partial = opts[:partial] template = find_partial(partial, opts) opts[:layout] = :none + + # Add an instance variable that can be used to create the locals in the + # partial + if opts[:locals] + @_merb_partial_locals = opts[:locals] + end + opts[:clean_context] = true when js = opts[:js] headers['Content-Type'] = "text/javascript" opts[:layout] = :none if String === js return js @@ -69,18 +79,20 @@ end when xml = opts[:xml] headers['Content-Type'] = 'application/xml' headers['Encoding'] = 'UTF-8' return xml - else + when template = opts[:template] + template = find_template(:template => template) + else template = find_template(:action => action) end engine = engine_for(template) options = { :file => template, - :view_context => (opts[:clean_context] ? clean_view_context : _view_context), + :view_context => (opts[:clean_context] ? clean_view_context : cached_view_context), :opts => opts } content = engine.transform(options) if engine.exempt_from_layout? || opts[:layout] == :none content @@ -90,11 +102,11 @@ end # this returns a ViewContext object populated with all # the instance variables in your controller. This is used # as the view context object for the Erubis templates. - def _view_context + def cached_view_context @_view_context_cache ||= ViewContext.new(self) end def clean_view_context ViewContext.new(self) @@ -127,12 +139,12 @@ # in the view directory that corresponds to the current # controller name. If you pass a string with a path in it # you can render partials in other view directories. So # if you create a views/shared directory then you can call # partials that live there like partial('shared/foo') - def partial(template) - render :partial => template + def partial(template, locals={}) + render :partial => template, :locals => locals end private def wrap_layout(content, opts={}) @@ -144,29 +156,30 @@ else layout_choice = find_template(:layout => :application) end end - _view_context.instance_variable_set('@_layout_content', content) + cached_view_context.instance_variable_set('@_layout_content', content) engine = engine_for(layout_choice) options = { :file => layout_choice, - :view_context => _view_context, + :view_context => cached_view_context, :opts => opts } engine.transform(options) end # OPTIMIZE : combine find_template and find_partial ? def find_template(opts={}) - if action = opts[:action] - path = - File.expand_path(MERB_ROOT / "dist/app/views" / self.class.name.snake_case / action) + if template = opts[:template] + path = _template_root / template + elsif action = opts[:action] + path = _template_root / self.class.name.snake_case / action elsif _layout = opts[:layout] path = _layout_root / _layout else raise "called find_template without an :action or :layout" - end + end extensions = [_template_extensions.keys].flatten.uniq glob = "#{path}.{#{opts[:ext] || extensions.join(',')}}" Dir[glob].first end \ No newline at end of file