Php 电子邮件功能使用模板。包括通过ob_开始和全局变量
我有一个简单的Email()类。它用于从我的网站发送电子邮件Php 电子邮件功能使用模板。包括通过ob_开始和全局变量,php,templates,global-variables,abstract-class,ob-start,Php,Templates,Global Variables,Abstract Class,Ob Start,我有一个简单的Email()类。它用于从我的网站发送电子邮件 <? Email::send($to, $subj, $msg, $options); ?> 为了呈现PHP变量,我必须在函数中包含模板。但是由于include不返回内容,而是直接将其发送到输出,因此我必须使用缓冲区函数对其进行包装: <? abstract class Email { public static function send($to, $subj, $msg, $options = arra
<?
Email::send($to, $subj, $msg, $options);
?>
为了呈现PHP变量,我必须在函数中包含模板。但是由于include
不返回内容,而是直接将其发送到输出,因此我必须使用缓冲区函数对其进行包装:
<?
abstract class Email {
public static function send($to, $subj, $msg, $options = array()) {
/* ... */
ob_start();
include '/inc/email/templates/account_created.php';
$msg = ob_get_clean();
/* ... */
}
}
亲爱的
...
所以问题是是否有更优雅的解决方案?我主要关心使用ob_start()
和global
的变通方法。出于某种原因,我觉得这很奇怪。或者这几乎是常见的做法?执行此操作的方法之一是将文件内容读取到变量,然后使用regexp替换占位符。例如,您有new-users-template.phtml。您使用$content=file_get_content('new-users-templae.phtml')阅读it内容代码>。在这些模板中,您将有占位符,如%%username%%
,%%sitename%%
,%%siteurl%%
。您需要使用替换占位符的str\u replace
来处理此内容。将此代码移动到某个“prepareEmailTemplate”
函数,并将此函数的结果放入“send”
函数。一个解决方案是将所需变量全球化,而不是在模板内,而是在函数send
内
public static function send($to, $subj, $msg, $options = array()) {
global $SITE_NAME, $SITE_URL, $name;
/* ... */
ob_start();
include '/inc/email/templates/account_created.php';
$msg = ob_get_clean();
/* ... */
}
另一个解决方案是将这些额外变量作为参数传递。这可能很难看,因为set
函数中的参数数量可能会根据模板中需要的参数数量而大量增加。为了解决这个问题,实现这个解决方案的另一种方法是将这些额外变量作为散列传递,并动态创建这些变量(使用函数eval
)。下面是一个例子:
public static function send($to, $extra_vars = array()) {
foreach ($extra_vars as $key => $value) {
eval("\$$key = '$value';");
}
/* ... */
ob_start();
include '/inc/email/templates/account_created.php';
$msg = ob_get_clean();
/* ... */
}
然后,当您应该像这样呼叫send时:
$SITE_NAME = "www.somewebsite.com";
Email::send("recipient", array('SITE_NAME' => $SITE_NAME));
您可以在中找到更优雅的问题解决方案。
请注意PHP函数用于实例化模板变量。
换句话说,您应该将模板解析逻辑移到电子邮件发送功能之外。
例如:
<?php
class SimpleTemplate {
private $_tpl = "";
private $_vars = array();
function __construct($tpl_name) {
$this->_tpl = $tpl_name;
}
public function __set($name, $value) {
$this->_vars[$name] = $value;
}
public function setVars($values) {
$this->_vars = $values;
}
public function parse() {
ob_start();
extract($this->_vars);
include $this->_tpl;
return ob_get_clean();
}
}
abstract class Email {
public static function send($to, $subj, $msg, $options = array()) {
/* ... */
}
}
$tpl = new SimpleTemplate('/inc/email/templates/account_created.php');
$tpl->name = 'Stack Overflow';
$tpl->SITE_NAME = 'site_name';
$tpl->SITE_URL = 'localhost';
Email::send("me@localhost", "Subject", $tpl->parse());
?>
Hmm。。这有点像smarty模板的工作原理,对吧?然而,无论如何,变量仍然需要在函数内部全球化。也许,我没有看到Smarty的代码:)关于变量-您可以将它们作为发送函数的附加参数提供。但是更好的选择是将模板解析的代码移到另一个函数中,并将结果内容放到发送函数中。实际上,您可以完全按照这种方式使用现有文件,替换PHP的整个部分,例如str_replace(“”,'Some name',$email_html)代码>我通常不喜欢全球化变量的想法。像您建议的那样在函数中全局化它们似乎更干净一些,但是当模板中引入新变量时,需要不断地修改类。那不好玩。我有点喜欢你的第二个想法,把它们放到send
函数中。我得试一下。但我绝对不推荐使用eval。我最好将模板更新为echo$extra\u vars['SITE\u NAME']
。非常感谢。祝贺你的声誉得分!!:)是的,eval可能是邪恶的;-)将它们作为数组放在模板中也很好
$SITE_NAME = "www.somewebsite.com";
Email::send("recipient", array('SITE_NAME' => $SITE_NAME));
<?php
class SimpleTemplate {
private $_tpl = "";
private $_vars = array();
function __construct($tpl_name) {
$this->_tpl = $tpl_name;
}
public function __set($name, $value) {
$this->_vars[$name] = $value;
}
public function setVars($values) {
$this->_vars = $values;
}
public function parse() {
ob_start();
extract($this->_vars);
include $this->_tpl;
return ob_get_clean();
}
}
abstract class Email {
public static function send($to, $subj, $msg, $options = array()) {
/* ... */
}
}
$tpl = new SimpleTemplate('/inc/email/templates/account_created.php');
$tpl->name = 'Stack Overflow';
$tpl->SITE_NAME = 'site_name';
$tpl->SITE_URL = 'localhost';
Email::send("me@localhost", "Subject", $tpl->parse());
?>