2008年9月14日星期日

Re: [fw-mvc] Using Zend_View/Layout for dynamic HTML mail

-- The WRS <thewrs@gmail.com> wrote
(on Sunday, 14 September 2008, 11:45 PM +0100):
<snip>
> The following is a snippet from the action which handles account registration.
> The entire action is responsible for checking the provided information,
> creating the account, generating the mail body, sending the mail, then
> rendering a 'registration successful' style page to the browser
>
> --
>
> $view = new Zend_View;
> $view->layout()->setLayout('email');
> $view->setScriptPath(APP_PATH.'email');
> $view->assign('token', $activation_key);
>
> $m = new Zend_Mail;
> $m->setBodyHtml($view->render('register.phtml'));
> $m->send();
>
> --
>
> This would appear to work correctly as far as sending the actual HTML mail is
> concerned, however it seems my calls to $view->layout()->setLayout() have
> affected the layout being using for the actual controller action making the
> resulting 'registration successful' web page being tainted.
>
> I hope this makes sense, I'm guessing that I can only have one instance of
> Zend_View and Zend_Layout. Resetting the layout script to the default after the
> mail is generated may be a solution but it leaves me to believe I may be going
> about this the wrong way.

Create a new instance of Zend_Layout. Zend_Layout is only a "pseudo"
singleton -- the startMvc() method registers an instance used with MVC
components, and injects it into the various helpers and plugins.
However, you can instantiate standalone versions of Zend_Layout easily:

$layout = new Zend_Layout(array('layoutPath' => ...));
$layout->setLayout('email');
$layout->content = $view->render('register.phtml');
$final = $layout->render();

> In short, how can I render and capture a view script while making use of
> layouts without affecting those which have been linked to my controller/action.

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: