简体   繁体   中英

What are the best practices to generate “widgetized” content in Rails

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM