Methods

Included Modules

Class Index [+]

Quicksearch

Ramaze::View::Erector

Adapter for Erector. Erector is a view engine that works a bit like Markably but offers a much more pleasant way of building your views. By creating classes in plain ruby you can generate layouts and views without having to write a single line of HTML.

Each layout or view is a simple class that matches the filename. A layout named “default.erector“ would result in a class with the name “Default”. It’s very important to know that you should ALWAYS extend Ramaze::View::Erector. Without extending this class you won’t be able to use Erector at all.

When working with the Erector adapter there are a few things you’ll need to know. First all your views and layouts should be classes as explained earlier on. Each class should have at least a single method named “content”. This method is executed by Erector and the HTML it produces will either be stored in the @content instance variable (if it’s a view) or sent to the browser if it’s a layout. The @content variable can be displayed by calling the rawtext() method and passing the variable as it’s parameter.

Using helper methods, such as the render_* methods is also possible although slightly different than you’re used to. Due to the way the Erector adapter works it isn’t possible to directly call a helper method. As a workaround you can access these methods from the “@controller” instance variable. Don’t forget to render the output of these helpers using rawtext(). Feel free to submit any patches if you think you have a better solution so that developers don’t have to use the @controller instance variable.

@example

  # This is the code for the layout
  class Default < Erector::Widget
    html do
      head do
        title 'Erector Layout'
      end

      body do
        rawtext @content
      end

    end
  end

  # And here's the view
  class Index < Erector::Widget
    def content
      h2 'This is the view'
    end
  end

  # Render an extra view
  class ExtraView < Erector::Widget
    def content
      rawtext @controller.render_view :some_extra_view
    end
  end

@author Yorick Peterse

Public Class Methods

call(action, string) click to toggle source

The call method is called whenever a view is loaded. A view can either be a layout or an actual view since they’re treated the same way. First the view is loaded, followed by the layout.

@author Yorick Peterse @param [Object] action Object containing a copy of the current Action

 class data.

@param [String] string The content of the currently loaded layout. This

 variable isn't used by the Erector adapter but is required since Ramaze
 expects 2 parameters. Usually this string is used to inline load (or
 evaluate) the content of a view.

@return [String] The generated HTML.

     # File lib/ramaze/view/erector.rb, line 87
 87:       def self.call action, string
 88:         # Return the contents unless a view has been defined
 89:         return string, 'text/html' unless action.view
 90: 
 91:         # Evaluate the class so we can use it. The content of "string"
 92:         # is a full blown class that should always have a "content" method.
 93:         #eval string, action.binding
 94:         eval string
 95: 
 96:         # Generate the class name based on the filename.
 97:         # Class names are a CamelCased version of the filename (without the
 98:         # extension).
 99:         klass    = File.basename action.view, '.erector'
100:         klass    = klass.camel_case
101:         view_obj = self.const_get(klass)
102: 
103:         # Synchronize the methods of action.instance with the view. These
104:         # methods can be accessed by calling @controller.METHOD
105:         action.variables[:controller] = action.instance
106: 
107:         # Now that we have all the data we can start rendering the HTML.
108:         # Note that we pass the action.variables hash to the new() method. This
109:         # is done to give the view access to all existing (instance) variables.
110:         # Syncing them using action.copy_variables didn't seem to do the trick.
111:         html = view_obj.new(action.variables).to_html
112: 
113:         # All done
114:         return html, 'text/html'
115:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.