-
Notifications
You must be signed in to change notification settings - Fork 151
Rendering Content
To support a variety of content types, Concerto 2 uses a series of partials to render content uniformly across views. These partials can, if needed, be backed with a variety of model methods or the use of the ContentController's display method to serve file data.
Anywhere you want to display actual content, like a graphic image or ticker text, you should be calling the render_content function like this:
render_content(@content, {:type => "grid"})
The @content (which doesn't need to be an instance variable) must be the Content instance you'd like to render (i.e a Graphic, Ticker, or other child class of Content). The type key in the following hash specifies what rendering instance is being called. The grid view would specify a grid type of rendering, a content#show page might use a show_page rendering, etc. These renderings (you can make them up on your own) must be lowercase non-breaking strings, so don't get tricky and try to put a space in there. You can pass additional parameters via the hash should you so desire, the entire thing will get sent along to the rendering partial.
When you request a render_content function with type "grid" the system is going to look at the content type (for example Graphic) and then look for the render_grid partial within the folder for that content type. In essence, if @content is a graphic, calling render_content(@content, {:type => "grid"})
will trigger:
render :partial => 'contents/graphic/render_grid', :locals => {:content => @content, :options => {:type => "grid"}
To catch this, you'll need to build contents/graphic/_render_grid.html.erb that responds appropriately to a local variable content. For a simple ticker text situation, /contents/ticker/_render_grid.html.erb might just look like:
<%= truncate(content.data) %>
These partials will need to be build for every type of content in the system. If you are authoring a new content plugin, you should see (insert link to list of partials we use) to figure out what partials you'll need to support in a regular Concerto 2 installation.
If you need to return a file somewhere as a result of your content rendering (i.e a resized image) your best bet is to define the rendering partial such that it references a file path (i.e put this in your image_tag for the image path) that we can control on a per-content-type level, like:
display_content_path(content, :width => 10, :height => 10)
The ContentController has a display method (and corresponding route) that will ask the the current piece of content to render and return a Media instance with the relavent file attributes (file_data, file_name, file_type) set. The render method must be defined on the Content model, so models/Graphic.rb might have a simple render method that looks like:
def render(options={})
require 'RMagick'
@media = self.medias.original.first
image = Magick::ImageList.new
image.from_blob(@media.file_contents)
image.resize!(options[:width].to_i, options[:height].to_i)
file = Media.new(
:attachable => self,
:file_data => image.to_blob,
:file_type => image.mime_type,
:file_name => @media.file_name
)
return file
end