电子邮件功能使用模板包括通过ob_start和全局变量 [英] Email function using templates. Includes via ob_start and global vars

查看:204
本文介绍了电子邮件功能使用模板包括通过ob_start和全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Email()类。它用于从我的网站发送电子邮件。

 < ;? 
电子邮件::发送($ to,$ subj,$ msg,$ options);
?>

我也有一堆电子邮件模板,用简单的HTML编写,带有几个PHP变量。例如。 /inc/email/templates/account_created.php

 < p>亲爱的<?= $ name?>,< / p> 
< p>感谢您在<?= $ SITE_NAME?>创建帐户。要登录,请使用下面的链接:< / p>
< p>< a href =https://<?= $ SITE_URL?> / accounttarget =_ blank><一个>< / p为H.

为了让PHP变量呈现,我不得不 include 模板放入我的函数中。但是由于 include 不会返回内容,而只是直接发送给输出,所以我不得不用缓冲区函数来包装它:

 <? 
抽象类电子邮件{
public static function send($ to,$ subj,$ msg,$ options = array()){
/ * ... * /
ob_start ();
包含'/inc/email/templates/account_created.php';
$ msg = ob_get_clean();
/ * ... * /
}
}

之后,我意识到PHP变量不会被渲染,因为它们在函数范围内,所以我必须全局化模板中的变量:

 < ;? 
全球$ SITE_NAME,$ SITE_URL,$ name;
?>
< p>亲爱的<?= $ name?>,< / p>
...

所以问题在于是否有更优雅的解决方案?主要是我担心我的解决方法是使用 ob_start() global 。出于某种原因,我觉得很奇怪。或者这是非常常见的做法?

https://stackoverflow.com/questions/3706855/send-email-with-a-template-using-php\">这个答案。

注意PHP extract 函数来实例化模板变量。

换句话说,您应该将电子邮件发送功能之外的模板解析逻辑移开。例如:

 < ?php 

class SimpleTemplate {
private $ _tpl =;
private $ _vars = array();

函数__construct($ tpl_name){
$ this-> _tpl = $ tpl_name;


公共函数__set($ name,$ value){
$ this-> _vars [$ name] = $ value;
}

公共函数setVars($ values){
$ this-> _vars = $ values;


public function parse(){
ob_start();
提取($ this-> _vars);
包含$ this-> _tpl;
return ob_get_clean();



抽象类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());

?>


I have a simple Email() class. It's used to send out emails from my website.

<?
Email::send($to, $subj, $msg, $options);
?>

I also have a bunch of email templates written in plain HTML pierced with a few PHP variables. E.g. /inc/email/templates/account_created.php:

<p>Dear <?=$name?>,</p>
<p>Thank you for creating an account at <?=$SITE_NAME?>. To login use the link below:</p>
<p><a href="https://<?=$SITE_URL?>/account" target="_blank"><?=$SITE_NAME?>/account</a></p>

In order to have the PHP vars rendered I had to include the template into my function. But since include does not return the contents but rather just sends it directly to the output, I had to wrap it with the buffer functions:

<?
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();
        /* ... */
    }
}

After that I realized that the PHP vars are not rendered as they are being inside of the function scope, so I had to globalize the variables inside of the template:

<?
global $SITE_NAME, $SITE_URL, $name;
?>
<p>Dear <?=$name?>,</p>
...

So the question is whether there is a more elegant solution to this? Mainly I am concerned about my workarounds using ob_start() and global. For some reason that seems to me odd. Or this is pretty much the common practice?

解决方案

You can find a more elegant solution to your problem in this answer.
Notice the usage of the PHP extract function to instantiate the template variables.
In other words, you should move the template parsing logic outside the e-mail sending function.
For example:

<?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());

?>

这篇关于电子邮件功能使用模板包括通过ob_start和全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆