views:

49

answers:

2

On my previous projects built with usage of Zend Framework I extensively used Zend_view's "Action View" helper. It basically allows to initiate a separate cycle of request->dispatch ti action->view_rendering from a view script.

Here is the link to an appropriate page of zend framework reference guide (search for "Action View Helper").

This helper is quite a convenient way to work with widgetized content for example on pages with portal layout where you can stuff a page with different widgets (like advertising blocks, currency informers etc).

Although such an approach negatively affects response time of the page as it involves a lot of additional routing/dispatching activity, it allows to organizes widget code and views scripts into a rational structure. One widget is just a controller action, with linked view script.

Unfortunatelly, I have not found any similar view helpers in Rails. Is there any way to elegantly solve this task in Rails?

A: 

In Rails, the render method of ActionController takes a Hash as arguments and outputs a simple string. You can use this in your view like so:

<%= render :partial => "shared/widgets/_#{widget.widgettype.name}", :collection => @current_user.widgets %>

In this case Rails will iterate through the list of @current_user.widgets, find a widget partials in "app/views/shared/widgets/_widgettypename.html.erb", and call the partial for each attached widget.

For further reading on Rails partials, see the ActionView::Partials documentation.

marshally
The recipe tells me about the actual way which I am trying to avoid. The problem is that widgets usually need some work to be done in models and controllers part before it can be passed to views.
cryo28
With this approach I need to prepare all the data in a controller, whose view will be calling widget partials. Of course a widget logic can be put into separate classes/helpers. But I need to link the actual page with a particular set of widgets in two separate places - in a controller which page is being rendered (to prepared widget data) and in the controller's view script (actual render calls). And I do not like it as its "ugly" to activate some code of a programm in two places in order for it to run properly.
cryo28
As I pointed out in the question - Zend Framework has solved such a task quite elegantly - by running a separate request->dispatch->controller_action->render cycle, which result (html) is just put into a place in a view script. So any widget is just a simple controller action with assoicated view script. But I can't figure out how to do it in Rails.
cryo28
RadiantCMS extensions have a feature similar to what you are describing from Zend. Basically each extension is like its own tiny little app. You might want to dig in to their source code to see how it works.
marshally
A: 

It seems that I've found an answer to my questions myself. It is "render_component" rails plugin. There is also a fork which apparently supports Rails3.

cryo28
Could not post two links in one answer - spam prevention mechanism blocked me to do so. Here is the link to the fork - http://github.com/vhochstein/render_component/
cryo28