我正在处理一个PHP类,在这里我用数据替换变量到HTML电子邮件模板文件。它通过将数据替换为“{{first_name}}”这样的字符串。通过str_replace()显示html模板中的数组数据

这样,我可以用一个客户的正确数据替换像first_name,last_name,email等变量。这对单值很好,但现在我有一个问题。

在这封电子邮件中,我展示了客户订购的产品。这是一个阵列产品,每个产品都有自己的规格(请看下面的示例数组)。

问题: 有没有人有一个想法,我可以如何实现用产品数组的循环替换{{variable}}?

产品阵列例如:

$products = array(

array(

'name' => 'Product 1',

'price' => 10.00,

'qty' => 1

),

array(

'name' => 'Product 2',

'price' => 12.55,

'qty' => 1

),

array(

'name' => 'Product 3',

'price' => 22.10,

'qty' => 3

)

);

我的类别:

class ConfirmationEmail {

protected $_openingTag = '{{';

protected $_closingTag = '}}';

protected $_emailValues;

protected $_template;

/**

* Email Template Parser Class.

* @param string $templatePath HTML template string OR File path to a Email Template file.

*/

public function __construct($templatePath) {

$this->_setTemplate($templatePath);

}

/**

* Set Template File or String.

* @param string $templatePath HTML template string OR File path to a Email Template file.

*/

protected function _setTemplate($templatePath) {

$this->_template = file_get_contents($templatePath);

}

/**

* Set Variable name and values one by one or at once with an array.

* @param string $varName Variable name that will be replaced in the Template.

* @param string $varValue The value for a variable/key.

*/

public function setVar($varName, $varValue) {

if(! empty($varName) && ! empty($varValue)) {

$this->_emailValues[$varName] = $varValue;

}

}

/**

* Set Variable name and values with an array.

* @param array $varArray Array of key=> values.

*/

public function setVars(array $varArray) {

if(is_array($varArray)) {

foreach($varArray as $key => $value) {

$this->_emailValues[$key] = $value;

}

}

}

/**

* Returns the Parsed Email Template.

* @return string HTML with any matching variables {{varName}} replaced with there values.

*/

public function output() {

$html = $this->_template;

foreach($this->_emailValues as $key => $value) {

if(! empty($value)) {

$html = str_replace($this->_openingTag . $key . $this->_closingTag, $value, $html);

}

}

return $html;

}

}

在动作:

$template_path = 'path-to-template/email-templates/confirmation.php';

$emailHtml = new ConfirmationEmail($template_path);

$emailHtml->setVars(array(

'first_name' => 'Jack',

'last_name' => 'Daniels',

'street' => 'First street',

'number' => '22',

// Other data

));

// Outputs the HTML

echo $emailHtml->output();

Ps。如果需要,我可以向您显示HTML电子邮件模板。这是一个包含内联样式和需要替换数据的地方{{variables}}的很多表格的html结构。

2015-09-08

Robbert

调用html模板显示数据,通过str_replace()显示html模板中的数组数据相关推荐

  1. 随机从mysql中读取_如何实现MySQL表数据随机读取?从mysql表中读取随机数据

    文章转自 http://blog.efbase.org/2006/10/16/244/ 如何实现MySQL表数据随机读取?从mysql表中读取随机数据?以前在群里讨论过这个问题,比较的有意思.mysq ...

  2. Python中用pandas将numpy中的数组数据保存到csv文件

    Python中用pandas将numpy中的数组数据保存到csv文件 本博客转载自:[1]https://blog.csdn.net/grey_csdn/article/details/7018587 ...

  3. 2017年的双十一又一次刷新了记录,交易创建峰值32.5万笔/秒、支付峰值25.6万笔/秒。而这样的交易和支付等记录,都会形成实时订单Feed数据流,汇入数据运营平台的主动服务系统中去。数据运营平台的

    2017年的双十一又一次刷新了记录,交易创建峰值32.5万笔/秒.支付峰值25.6万笔/秒.而这样的交易和支付等记录,都会形成实时订单Feed数据流,汇入数据运营平台的主动服务系统中去.数据运营平台的 ...

  4. mfc编辑框显示数据_Excel表格技巧—Excel表格中怎么给数据分等级

    我们处理数据的时候,经常需要给数据分等级,想要在excel里自动划分等级,用到函数就能很快实现.下面我们以划分一组学生成绩等级为例,来学习一下怎么给Excel表格中的数据分等级吧! 第一步:打开需要显 ...

  5. ssrs 数据分页_如何在SSRS中使用JSON数据

    ssrs 数据分页 In this article, we will explore the concept of using JSON data in SQL Server Reporting Se ...

  6. excel如何晒出重复数据_3秒找出Excel中的重复数据,年底数据统计用得上,赶紧码住...

    职场人经常会碰到数据核对的任务,特别是在月底和年末,老板索要数据统计结果时,你总得检查一遍再交上去吧. 如果你对 Excel 中的基本功能不熟悉,就很可能碰到和这位妹子一样的难题: 遇到上面这种情况该 ...

  7. matlab存储excel数据,怎么把matlab表格数据写入excel-怎么把matlab中处理的数据存入到excel中...

    怎么把matlab中处理的数据存入到excel中 1.上双开matlab 2.接着可以直接在命进行相关的操作 3.也可以直接新建一个脚本,我这里是直接在命令行进行的 4.现在我们介绍一下关于将数据变量 ...

  8. mysql在视图中增加新数据_怎么向Mysql视图中增加新数据

    本篇文章主要给大家介绍mysql数据表中视图是怎么新增数据的. mysql视图的相关知识在我们之前的文章中,都已经给大家详细介绍过了,相信大家对视图肯定有了更深一步的了解. 从前面文章的介绍中,大家应 ...

  9. java性别分类汇总,excel表格数据男女分类汇总-在Excel中,对数据清单进行“按性别分类汇总出男女......

    在Excel中,对数据清单进行"按性别分类汇总出男女... 在Excel中,对数据清单进行性别分类汇总出男女生的英语平均分"可使用AVERAGEIF函数实现. 操骤如下: 1.打开 ...

最新文章

  1. 《Java程序员,上班那点事儿》图书有奖征文图书奖品名单
  2. C#调用ATL COM
  3. 共聚焦图片怎么加标尺_科研教程|利用PS给电镜加标尺
  4. mysql .pdb是什么文件_超详细讲解如何使用 pdb 在服务器上调试代码
  5. go语言的iota是什么意思_go语言基础之iota枚举
  6. 【Vue3.0】—V- model‘ directives require no argument.
  7. 广播接收者android,电话拦截广播,电话接收者demo
  8. Linux yum安装java环境
  9. python plot坐标轴_修改python plot折线图的坐标轴刻度方法
  10. atitit.软件设计模式大的总结attialx总结
  11. 计算机丢失msvcrt.dll,msvcrt.dll修复工具
  12. 使用谷歌地图拾取异国坐标
  13. Outlook邮箱不简单带你重新认识它
  14. 这届年轻人爱换“QQ秀”的中二病还能治好吗?
  15. sko.GA实现遗传算法的源码解析
  16. ArcGISPro通视分析之视线分析
  17. pandas数据处理:常用却不甚了解的函数,pd.read_excel()
  18. 小学计算机课题研究方案,《小学信息技术课堂有效教学的探究》课题研究方案...
  19. 某音热门---图片转字符SpringBoot版
  20. 唐伯虎的诗,可能是改编的

热门文章

  1. 大学计算机实验报告示例,实验报告范例(学生).doc
  2. 手机能打开的表白代码_手机待机时间变短,掉电快?一串隐藏代码就能给手机补电...
  3. word学习(一):交叉引用和题注学习
  4. 开源项目-旅游信息管理系统
  5. java实现stg游戏_一些Java小游戏源码
  6. 搬家公司哪家最便宜?跨省跨市搬家
  7. 云服务器需要设置虚拟内存,云服务器需要设置虚拟内存
  8. 怎么让内网显示云服务器ip,腾讯云内网IP怎么用
  9. Lua 下的依赖注入
  10. 基于PHP的高效协同办公管理系统