Sha256: 4d19e30f87020b84253885f638db3c3055b07e7b4dcf3248c9c2a7ef16ab82cb

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

class Roda
  module RodaPlugins
    # The padrino_render plugin adds rendering support that is
    # similar to Padrino's.  While not everything Padrino's
    # rendering supports is supported by this plugin (yet), it
    # currently handles enough to be a drop in replacement for
    # some applications.
    #
    # Most notably, this makes the +render+ method default to
    # using the layout, similar to how the +view+ method works
    # in the render plugin.  If you want to call render and not
    # use a layout, you can use the <tt>:layout=>false</tt>
    # option:
    #
    #   render('test')                 # layout
    #   render('test', :layout=>false) # no layout
    #
    # This also adds a +partial+ method, which renders templates
    # without the layout, but prefixes the template filename to
    # use with an underscore:
    #
    #   partial('test')     # uses _test.erb
    #   partial('dir/test') # uses dir/_test.erb
    #
    # 
    module PadrinoRender
      OPTS = {}.freeze
      SLASH = '/'.freeze

      # Depend on the render plugin, since this overrides
      # some of its methods.
      def self.load_dependencies(app, opts=OPTS)
        app.plugin :render, opts
      end

      module InstanceMethods
        # Call view with the given arguments, so that render
        # uses a layout by default.
        def render(template, opts=OPTS)
          view(template, opts)
        end

        # Renders the given template without a layout, but
        # prefixes the template filename to use with an 
        # underscore.
        def partial(template, opts=OPTS)
          opts = parse_template_opts(template, opts)
          if opts[:template]
            template = opts[:template].split(SLASH)
            template[-1] = "_#{template[-1]}"
            opts[:template] = template.join(SLASH)
          end
          render_template(opts)
        end
      end
    end

    register_plugin(:padrino_render, PadrinoRender)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roda-2.1.0 lib/roda/plugins/padrino_render.rb
roda-2.0.0 lib/roda/plugins/padrino_render.rb