Twig 模板中未替换的变量 [英] Variables not replaced in Twig template
本文介绍了Twig 模板中未替换的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个 PHP 代码(仅使用 Twig + SwiftMailer):
I have this PHP code (just using Twig + SwiftMailer):
# Load Twig and swiftmailer
require_once('lib/SwiftMailer/swift_required.php');
require_once('lib/Twig/Autoloader.php');
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader, array(
'cache' => 'cache',
));
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
# Render twig template to a string
$template = $twig->loadTemplate('email.twig');
$body = $template->renderBlock('body', array('name' => $_POST["name"], 'letter' => $_POST["letter"]));
$message = Swift_Message::newInstance()
->setFrom("info@mydomain.com")
->setSubject("Hi, ".$_POST["name"]." blahblahblah")
->setBody($body, 'text/html')
->setTo($_POST["email"]);
# Send email, using PHP mail function
$retval = $mailer->send($message);
关于我的 email.twig 模板:
About my email.twig template:
- 它包含 {{ name }} 和 {{ letter }}
- 它包含在 {% block body %} ... {% endblock %} 之间
- 它有一个
节点,带有一些 CSS 样式.
- It contains {{ name }} and {{ letter }}
- It's contained between {% block body %} ... {% endblock %}
- It has a
<style>
node, with a few CSS styles.
更新:模板链接此处
电子邮件已正确发送,但存在 2 个问题:
The email is sent properly, but with 2 problems:
- 第二个变量 letter 未被替换.它是空的,即使 $_POST['letter'] 不为空
- 并非所有 CSS 样式都正确应用.我想我不能在电子邮件中使用所有 CSS 属性.我说得对吗?
- The second variable, letter, is not replaced. It's empty, even when $_POST['letter'] is not empty
- Not all CSS styles are properly applied. I guess I can't use all the CSS properties in an email. Am I right?
有什么线索吗?
推荐答案
我遇到了类似的问题并找到了解决方案:这是由于模板中的一些不可打印字符造成的.不确定它对您来说是否相同,但也许您应该尝试重写 {{ letter }} 部分.(我的法语键盘通过 Alt-Gr + 空格的组合插入了一个隐形字符).
I had a similar problem and find a solution : that was due to some non-printable chars in the template. Not sure that it's the same for you, but maybe you should just try to rewrite the {{ letter }} part. (My french keyboard insert an invisible char with the combination of Alt-Gr + space).
这篇关于Twig 模板中未替换的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文