[Solar-talk] Composite views
Paul M Jones
pmjones at ciaweb.net
Fri Nov 2 19:13:45 CDT 2007
On Nov 2, 2007, at 6:19 PM, Rodrigo Moraes wrote:
> I've been using something to have some kind of components or gadgets
> "detached" from a controller/view. It is a mix of view and controller
> helpers, and the best to explain is to use an example. In a view, I
> call:
>
> <?php echo $this->widget('News')->latestNews(); ?>
>
> * 'widget' is a view helper that just returns a "factory"
> controller helper.
> * the controller helper builds the 'News' object, sets the view and
> page controller as a property of it, and returns it.
>
> (at this point, the News object has access to public controller
> properties - plus controller helpers - and has a view object to work
> with)
>
> * 'latestNews' fetches some data from a model and returns the result
> using partials.
> * the view template paths are unset and set back to original before
> and after 'latestNews', so that 'News' can use its own template
> directories stored relatively to it.
>
> The prize: I can call the 'latestNews' box in any controller. It is
> independent and takes care of everything it needs (data, templates).
>
> Now, what do you guys think? Is this too much overhead? What other
> tricks could we use to have this "modularity"?
I would argue that invoking any sort of business logic from the view
is probably not a good idea. Instead, I'd say the controller should
get the widget output and assign it to the view. So in the controller:
$widget = Solar::factory('Vendor_Widget_News');
$this->latest_news = $widget->latestNews();
And then in the view:
echo $this->latest_news;
Looking at it now, it appears like any other controller/view setup.
In fact, if the widgets were just other page controllers, you might
do this:
$widget = Solar::factory('Vendor_App_News');
$this->latest_news = $widget->fetch('latest'); // hits the
actionLatest() method
Having said that, I think the overhead of another page-controller
would be too heavy for our purposes here (don't really need another
whole dispatch cycle with the hooks and layouts and everything). So
a separate widget series, where the methods do the business logic and
then return the results of a widget view script without any layout,
would seem like a decent approach. (But call them from the
controller, not the view.)
Hope I'm not missing the point, and that I made sense. :-)
--
Paul M. Jones <http://paul-m-jones.com>
Solar: Simple Object Library and Application Repository
for PHP5. <http://solarphp.com>
Join the Solar community wiki! <http://solarphp.org>
Savant: The simple, elegant, and powerful solution for
templates in PHP. <http://phpsavant.com>
More information about the Solar-talk
mailing list