inline rendering of template + layout in symfony
I'm trying to process html email by rendering a template and layout inline, and sending the result. I can get a template to render inline, but wrapping it in a layout isn't working. This is what I have so far:
$templ开发者_如何学Cate = new sfPartialView(sfContext::getInstance(), 'email', 'send', 'my_template');
$template->setTemplate('my_template');
$template->setDecoratorTemplate('my_layout');
$email = $template->render();
Short answer:
You're missing $template->setDecorator(true)
.
Long answer:
I extend sfMailer
and add a function called composeAndSendPartial that will send a partial to the provided email address. If you extend sfMailer
, make sure you update your factories.yml.
/**
* Composes and sends an email with a body from rending $partial with $parameters
* @param string $from From address
* @param string $to To address
* @param string $subject Email subject
* @param string $partial The partial to render. Can be in the form module/template, or simply template. If no module is provided,
* module "email" is assumed
* @param array $parameters An array of parameters to render the partial with
*/
public function composeAndSendPartial($from, $to, $subject, $partial, $parameters = array())
{
if (false !== $sep = strpos($partial, '/'))
{
$moduleName = substr($partial, 0, $sep);
$templateName = '_' . substr($partial, $sep + 1);
}
else
{
$moduleName = 'email';
$templateName = '_' . $partial;
}
$view = new sfPHPView($context, $moduleName, $templateName, '');
$view->getAttributeHolder()->add($parameters);
$view->setDecorator(true);
$view->setDecoratorTemplate('email.php');
$html = $view->render(); //the contents of the rendered template
$this->composeAndSendHtml($from, $to, $subject, $html); //properly sets email formats, attaches plain text version, etc.
}
精彩评论