symfony中模板+布局的内联渲染

时间:2010-06-22 17:34:09

标签: email templates symfony1

我正在尝试通过内联呈现模板和布局来处理HTML电子邮件,并发送结果。我可以获得一个内联渲染模板,但将其包装在布局中是行不通的。这就是我到目前为止所做的:

$template = new sfPartialView(sfContext::getInstance(), 'email', 'send', 'my_template');
$template->setTemplate('my_template');
$template->setDecoratorTemplate('my_layout');
$email = $template->render();

1 个答案:

答案 0 :(得分:1)

简短回答

你错过了$template->setDecorator(true)

答案很长

我扩展sfMailer并添加一个名为composeAndSendPartial的函数,该函数将部分发送到提供的电子邮件地址。如果您扩展sfMailer,请确保更新factory.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.
      }