Method: YMDP::ApplicationView#render

Defined in:
lib/ymdp/view/application_view.rb

#render(params) ⇒ Object

Renders a partial into the current view. HTML partial names must be preceded with an underscore.

Rendering an HTML partial

HTML partials are located in app/views. HTML view files can be Haml or ERB. Haml is recommended and takes preference. HTML partials are named _filename.html.haml or _filename.html.erb.

render :partial => 'sidebar'

will find app/views/_sidebar.html.haml or app/views/_sidebar.html.erb and render its contents into the current view.

Specify a full path to indicate a specific template.

render :partial => 'sidebar.html.erb'

will find app/views/_sidebar.html.erb' and render it even if app/views/_sidebar.html.haml exists.

render :partial => 'shared/sidebar'

will find app/views/shared/_sidebar.html.haml and render its contents into the current view.

Rendering a JavaScript partial

You can render a single JavaScript file or send an array to concatenate a set of JavaScript files together asone script block.

Rendering a single JavaScript partial

JavaScript partials are located in app/javascripts and are named filename.js

render :javascript => 'application'

will find app/javascripts/application.js and render its contents into the current view in an inline script block.

render :javascript => 'shared/sidebar'

will find app/javascripts/shared/sidebar.js and render its contents into the current view in an inline script block.

Rendering a stylesheet partial

Stylesheets are located at app/stylesheets and are named filename.css

Rendering multiple partials

Pass an array to render to combine multiple files into a single inline block. This is useful for compression and validation, as it allows a set of files to be compressed or validated in a single context.

render :javascript => ['application', 'flash', 'debug']

will combine the contents of app/javascripts/application.js, app/javascripts/application.js, and app/javascripts/application.js into a single script block in the current view.

Pass a :filename parameter to set the name of the combined file. Currently the combined file only exists on disc while it’s being compressed and/or validated, but in the future this may be expanded to save multiple files as a single external asset.

render :javascript => ['application', 'flash', 'debug'], :filename => 'javascripts'

Currently the :filename parameter is simply a convenience.

Multiple partials of any type can be rendered.

For example:

render :partial => ['header', 'footer', 'sidebar'], :filename => 'html_layouts'

will find app/views/_header.html.haml, app/views/_footer.html.haml, and app/views/_sidebar.html.haml and write them to a temporary file called tmp/html_layouts before rendering that file into the current view.

This feature is intended mainly for JavaScript and CSS.

For example:

render :stylesheet => ['application', 'colors'], :filename => 'styles'

will render app/stylesheets/application.css and app/stylesheets/colors.css as a single temporary file called tmp/styles before rendering that file into the current view.

If compression and validation options are turned on, the resulting temporary file will be compressed and/or validated before being rendered into the current view. This will result in a more efficient compression and a more effective validation.



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ymdp/view/application_view.rb', line 188

def render(params)
  output = []
  
  unless params.has_key?(:tags)
    params[:tags] = true
  end
    
  output << render_html_partial(params)
  output << render_javascript_partial(params) if params[:javascript]
  output << render_stylesheet_partial(params) if params[:stylesheet]
  
  output.flatten.join("\n")
end