【问题标题】:Using Smarty Templates to send emails使用 Smarty 模板发送电子邮件
【发布时间】:2012-01-24 09:04:45
【问题描述】:

我有一个名为 Template 的表,我将消息设置为通过邮件发送,我需要输入 php 变量。

我的问题是:从表格中检索到消息后,我可以使用 Smarty 来编辑我的消息(它们是 HTML 代码)吗?

例如,我有:

$message = getMsgFromTable(Template);

$message 具有包含 php 变量的 Text 类型。我可以使用 Smarty 像这样填充这个变量吗?

$message = setVariableValueBySmarty($message);

【问题讨论】:

    标签: php mysql smarty


    【解决方案1】:

    Smarty 可以获取模板并返回其输出而不是显示它。
    fetch() 方法可以做到这一点,而这正是您所需要的:

    http://www.smarty.net/docs/en/api.fetch.tpl

    文档中还有一段告诉你如何使用它来发送邮件:)

    http://www.smarty.net/docs/en/api.fetch.tpl#id627712

    如果你需要从数据库中创建模板(存储为字符串),看看这个:

    http://www.smarty.net/docs/en/resources.string.tpl

    【讨论】:

    • 非常感谢 lorenzo-s ;)
    • 抱歉,我认为我可以接受多个答案;)
    • 当您决定哪个答案对您最有帮助时,通过单击答案左侧的复选框轮廓将其标记为已接受的答案。(来自How to ask FAQ)
    • 好的 :p 我使用这个代码:$smarty->assign('foo','value'); $template_string = '在此处显示 {$foo}'; $smarty->display('string:'.$template_string);那么我怎么能得到结果:s !!这是真的吗: $message = $smarty->display('string:'.$template_string); ???
    • @BFK 是的,我认为这是正确的。用assign()分配所有变量(和Smarty一样),然后从数据库中加载模板并分配给$template_string,最后使用$message = $smarty->display('string:'.$template_string);
    【解决方案2】:

    见@lorenzo-s 和:

    如果您的 DB 存储模板包含 <?php echo $foo; ?> 形式的“PHP 变量”(或类似的,没关系),您可能希望使用 prefilter<?php echo $foo; ?> 转换为 {$foo},这样您可以使用 Smarty 的 assign() 方法用您的数据填充模板。

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多