对于Zend_Framework,我想知道在HTML中构建内容的最佳实践是什么。在我的例子中,发送的电子邮件的内容是由许多因素决定的,例如数据库中某个特定值返回的行数。正因为如此,对于我来说,内容建立在发送电子邮件的控制器中是有意义的,该控制器与相关的数据库模型对话,并确定内容应该是什么。我不确定这是因为我们的设计师和版权人经常想要在电子邮件中调整副本,这将要求他们对模型进行修改或要求我这样做。我应该以不同的方式处理这件事吗?我是否应该将HTML片段存储在包含不同文本的某个地方,然后以某种方式调用它们?
编辑从消防员的答案,这是可以接受的做法,这样做。在视图中创建一个名为"partials“的文件夹,并使用它存储文本/html片段,然后我可以在需要的地方调用这些片段,并使用regexp(或类似的)用动态值替换特殊的字符串。
$nview = new Zend_View();
$nview->setScriptPath(APPLICATION_PATH.'/views/partials/');
$bodytext = $nview->render('response.phtml');
$mail = new Zend_Mail();
$mail->setBodyText($bodytext);
// etc ...例如,在这种情况下,根据模型返回的变量,可以使用两个不同的模板:
// within a controller
public function emailAction()
{
$images = new Model_ApplicationImages();
$totimages = count($images->fetchImages($wsid));
$acceptedImages = $images->fetchImages($wsid,'approved');
$accepted = count($acceptedImages);
$rejectedImages = $images->fetchImages($wsid,'rejected');
$rejected = count($rejectedImages);
$response = ($rejected == $totimages)?'rejected':'approved';
$nview = new Zend_View();
$nview->setScriptPath(APPLICATION_PATH.'/views/partials/');
$content = $nview->render($response.'.phtml');
$mail = new Zend_Mail();
$mail->setBodyText($content);
// etc
}有什么更优雅的方法可以/应该这样做吗?
发布于 2010-01-20 13:49:00
不确定这是否是最佳实践,但我所做的是使用以下方法扩展Zend_Mail:
setTemplatePath( $templatePath );
setTemplateHtml( $templateHtml );
setTemplateText( $templateText );
setTemplateArguments( array $templateArguments );...then在我覆盖的send()中的某个时刻是这样的:
$view = new Zend_View();
$view->setScriptPath( $this->_templatePath );
foreach( $this->_templateArguments as $key => $value )
{
$view->assign( $key, $value );
}
if( null !== $this->_templateText )
{
$bodyText = $view->render( $this->_templateText );
$this->setBodyText( $bodyText );
}
if( null !== $this->_templateHtml )
{
$bodyHtml = $view->render( $this->_templateHtml );
$this->setBodyHtml( $bodyHtml );
}因此,要利用这一点,您可以执行如下操作:
$mail = new My_Extended_Zend_Mail();
$mail->setTemplatePath( 'path/to/your/mail/templates' );
$mail->setTemplateHtml( 'mail.html.phtml' );
$mail->setTemplateText( 'mail.text.phtml' );
$mail->setTemplateArguments(
'someModel' => $someFunkyModel,
/* etc, you get the point */
)
$mail->send();换句话说,你可以让你的设计师和文案编辑视图(模板),就像他们已经习惯的那样。希望这对你有帮助,并激励你想出一些适合你需要的时髦的东西。
PS:
由于您提到了任意数据行,例如,您可以利用ZF附带的partialLoop视图助手。但你可能已经知道了?
PPS:
实际上,我同意螯合茨关于不扩展Zend_Mail,而是将其封装在自己的组件中的评论。
https://stackoverflow.com/questions/2101414
复制相似问题