用 PHP 变量替换分隔符
我正在编写一个邮件类,它会提取数据库中存储的内容并将其加载到模板中,然后再将其作为 HTML 电子邮件发送。但是,由于每封电子邮件都包含 PHP 变量和动态内容,因此我决定使用分隔符。因此,内容不会像这样:
Hello $username, welcome to the site.
它将看起来像:
Hello {{username}}, welcome to the site.
到目前为止,我正在使用这些方法:
function load($name,$content)
{
// preps the template for HTML
}
function content($template_id)
{
$template = $this->db->get_where('email_templates',array('id'=>$template_id));
return $template->content;
}
function new_email($email,$name,$user_type)
{
$msg = $this->load($name,$this->content(1));
$this->send($email,'Thanks for your application',$msg,1);
}
我遇到的麻烦是如何将 {{variable}} 转换为 $variable 以便它能够解析 - 我不希望它只是作为电子邮件模板中的 $username 加载。这只是使用正则表达式并转义字符串以便解析的情况吗?比如:
$content = str_replace("{{","'.$",$template->content);
$content = str_replace("}}",".'",$template->content);
或者这有缺陷吗?有人知道最好的做法是什么吗?
I'm writing a mail class that pulls content stored in a database and loads it into a template before sending it as a HTML e-mail. However, because each e-mail contains PHP variables and dynamic content, I've decided to use delimiters. So instead of the content looking like:
Hello $username, welcome to the site.
It'll look like:
Hello {{username}}, welcome to the site.
So far I'm using these methods:
function load($name,$content)
{
// preps the template for HTML
}
function content($template_id)
{
$template = $this->db->get_where('email_templates',array('id'=>$template_id));
return $template->content;
}
function new_email($email,$name,$user_type)
{
$msg = $this->load($name,$this->content(1));
$this->send($email,'Thanks for your application',$msg,1);
}
The trouble I'm having is how to convert a {{variable}} into a $variable so that it will parse - I don't want it to just be loaded as $username in the e-mail template. Is it just a case of using regular expressions and escaping the string so that it'll parse? Something like:
$content = str_replace("{{","'.$",$template->content);
$content = str_replace("}}",".'",$template->content);
Or is this flawed? Does anybody know what's the best thing to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不会创建自己的模板系统,因为已有的模板系统存在。
最流行的可能是 Smarty,但还有另一种格式与您创建的格式相同,即小胡子。
更新:
您的代码的问题在于您将
{{
替换为.$
并将其存储在$content 中
变量,然后将}}
替换为.
并覆盖此替换的$content
变量。一个可能的可行解决方案可能是:
但是您还需要以某种方式
eval
您的代码。I would not create my own templating system, because there are existing ones out there.
The most popular is probably Smarty, but there is an another one which has the same format as you created, that is mustache.
Update:
The problem with your code is that you're replacing the
{{
to a.$
and store that in$content
variable, then replacing}}
to.
and overwrite this replaced$content
variable.A possible working solution could be:
But then you would also need to
eval
your code somehow.因此,在电子邮件模板中将
{{variable}}
转换为$variable
后,您将使用eval
将其替换为实际内容那个变量?为什么不立即将
{{variable}}
替换为$variable
的内容呢?也许有一个函数可以接受模板文本和一个占位符=>的数组。 “替换它的文本”。然后,它就像通过在该数组的键周围添加
{{
和}}
并执行str_replace
来组成占位符的确切字符串一样简单。将其与占位符的(类)常量结合起来,您就拥有了一个非常可靠且防拼写错误的模板系统。它不会像完整的模板解决方案那样优雅或易于使用,并且可能需要编写使用它的代码的人进行额外的工作,但他们不会在开发过程中由于命名错误的变量而犯错误。
So after converting
{{variable}}
to$variable
in your email template, you will useeval
to get it replaced by the actual contents of that variable?Why not just replace
{{variable}}
with the contents of$variable
straight away?Perhaps have a function that takes the template text and an array of
placeholder => "text to replace it with"
. Then it's as simple as making up the placeholders' exact strings by adding{{
and}}
around that array's key and doingstr_replace
.Couple this with (class) constants for the placeholders and you have a very solid and typo-repelant templating system. It will not be as elegant or easy to use as a full blown templating solution, and it might require extra work from whoever writes code that uses it, but they will not make mistakes during development due to mis-named variables.
如果您打算自己执行此操作,最好使用 str_replace 进行明确。如果您尝试将大括号转换为 $,则需要 eval(),这是一个潜在的安全漏洞。
这将是我使用 str_replace 的方法 - 当您添加更多变量时,这会变得很难维护,但它实际上也没有变得更简单。
If you are going to do it yourself it is probably best to just be explicit with str_replace. If you try to convert the curly bracers to $ you'll then need to eval() which is a potential security hole.
This would be my approach with str_replace - this becomes difficult to maintain as you add more variables but it really doesn't get much simpler either.
使用 preg_replace_callback ,请参阅:http://codepad.org/EvzwTqzJ
use preg_replace_callback , see : http://codepad.org/EvzwTqzJ
归功于 https://www.labnol.org/code/19266-php-templates< /a>
Credit to https://www.labnol.org/code/19266-php-templates