Using a Zend Partial Inside a Model
Zend's partial view helper is great because it allow you to reuse the same HTML code repeatedly in multiple layouts and views but it can be used for so much more. For example, I'm working on a project that sends a large amount of email. Usually, I want to send these emails in response to something happening inside a model (new user signup, password reset, etc.). Ideally, I would be able to generate the email using the partials and not by inlining all the HTML in the model (which would have been my old approach) but you can't access the partial view helper directly from inside the model.
The code below creates a new Zend_View and sets it up so the partial view helper will work. I send out both an HTML and a text body for the emails so I create two separate view files so it's still easy to maintain.
$view = new Zend_View();
$view->setBasePath(APPLICATION_PATH . '/views/');
$emailBodyHTML = $view->partial('email/registrationEmail.phtml', array('values'=>$values));
$emailBodyText = $view->partial('email/registrationEmail.txt', array('values'=>$values));
To simplify things even more in my actual code base, I have extended Zend_Email so we only need to call one function to get this functionality.
$mail = new Application_Model_Email();
$email->setEmailBoth('registrationEmail', array('values'=>$values));
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows