(on Monday, 20 April 2009, 04:03 PM -0400):
> I'm trying to figure out the best way to handle slightly (very
> slightly) complex views with Zend and constantly running into a road
> block. I'm hoping you guys can help guide me down the right path
> here.
>
> Basically, I have several elements that get used in different parts of
> the system in different ways. The elements are things like Twitter
> updates, blog posts, current media, etc. Each element has its own
> view script which I want to include at higher levels. For instance,
> twitter updates get displayed in different places on my index page
> than they do on my dedicated Twitter page, but the basic view is the
> same in all cases. It's the layout that is different.
The easiest solution to this common problem for some reason is often
overlooked here.
In your action's main view script, have it specify the alternate layout:
<?php $this->layout()->setLayout('alternate.phtml'); ?>
Zend_Layout will look for that layout script in the layout script
directory you specified when instantiating Zend_Layout. (This is just
like resolving view scripts.)
In that layout script, simply call render() or partial() to include
those view scripts that pull in the elements you need:
<?php // if no clean variable scope is necessary, render(): ?>
<?php echo $this->render('twitterfeed.phtml'); ?>
<?php // if a clean variable scope is necessary, partial(): ?>
<?php echo $this->partial('blogposts.phtml', null, array('foo' => 'bar'); ?>
You can then render the various scripts you need in the appropriate
positions in the final layout, as well as the final content.
That's really all you need to do. If you need something more complex,
consider making view helpers.
> Here is some pseudo-code. I'm primarily concerned with how to handle
> the view aggregation from the controller side.
>
> TwitterController extends Zend_Controller_Action
> {
> public function recentAction()
> {
> $this->view->headLink()->appendStylesheet('css/twitter.css');
> $twitter = new Zend_Service_Twitter('username','password');
> $this->view->tweets = $twitter->status->userTimeLine('me');
> }
> }
>
> recent.phtml
> <?php foreach ($this->tweets as $tweet) { ?>
> <div class="tweet">
> <p><?= $tweet->text ?></p><br/>
> <em>via <?= $tweet->source ?></em>
> </div>
> <?php } ?>
>
>
> IndexController extends Zend_Controller_Action
> {
> public function indexAction()
> {
> $this->_helper->actionStack('recent', 'twitter');
> $this->_helper->actionStack('curmonth', 'blog');
> $this->_helper->actionStack('curlistening', 'media');
> $this->_helper->actionStack('curwatching', 'media');
> $this->_helper->actionStack('curreading', 'media');
> }
> }
>
> index.phtml
> <div id="blog-col">
> <?= $this->blog ?>
> </div>
>
> <div id="whatnow-col">
> <?= $this->tweets ?>
> <?= $this->songs ?>
> <?= $this->shows ?>
> <?= $this->books ?>
> </div>
>
>
> Obviously, actionStack doesn't work like I want it to. What I would
> REALLY like, is something along these lines...
>
> IndexController extends Zend_Controller_Action
> {
> public function indexAction()
> {
> $this->view->tweets = $this->_helper->actionStack('recent', 'twitter');
> $this->view->blog = $this->_helper->actionStack('curmonth', 'blog');
> $this->view->songs =
> $this->_helper->actionStack('curlistening', 'media');
> $this->view->shows =
> $this->_helper->actionStack('curwatching', 'media');
> $this->view->books = $this->_helper->actionStack('curreading', 'media');
> }
> }
>
>
> Thank you!!
>
> --
>
> Ryan Hagan
> ryan@ryanhagan.net
>
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
没有评论:
发表评论