使用CodeIgniter的word_censor()替换文件中的值

时间:2013-12-23 17:47:48

标签: php codeigniter email templates

我想知道这是CI的最佳做法,还是有更好的方法。 在我的项目中,我将在注册过程中发送确认电子邮件。我有一个HTML和TXT模板。它们都包含这样的东西:

Hello {USERNAME}, please click on the activation link below to finish the registration. Link: {LINK}

在我的方法中,我使用文件助手打开模板,然后使用文本助手的{}方法将word_censor()字符串替换为实际值,如下所示:

$tmpName = '{USERNAME}';
$tmpLink = '{LINK}';

$name = 'Jon' //retrieved from registration from
$link = 'mysite.com/activate/239dwd01039dasd' //runs the activate function providing the unique ID as an argument

$template = word_censor($template, $tmpName, $name);
$template = word_censor($template, $tmpLink, $link);

return $template

然后我就把$模板放在CI的邮件助手里面,如下所示:

$this->email->message($template);

我想知道的是,这是用我自己的值替换html / txt文件内容的最佳方法,还是有更好,更有效的方法来实现相同的结果。我只是不喜欢我使用word_censor()函数来做除了它的目的之外的事情。

1 个答案:

答案 0 :(得分:0)

更好的方法是将电子邮件模板存储为视图文件。

视图

Hello <?php echo $username; ?>, please click on the activation link below to finish the registration. Link: <?php echo $link; ?>

控制器

$data['username'] = 'Jon';
$data['link'] = 'mysite.com/activate/239dwd01039dasd';

$email_template = $this->load->view('myfile', $data, true);
$this->email->message($email_template);

view()上的第三个参数设置为true将返回模板,而不是将其回显。