您好我在db中有一个模板存储:如下:
<p>Dear [[OWNER_NAME]],</p>
<p><br />
We are thanks put [[LOCATION_NAME]] to you.</p>
<p>It is very early stagesyou know.</p>
<p>If anything comes of it, we will be in contact immediately with a further email.</p>
<p>Best wishes,</p>
<p>[[CUSTOMER]]</p>
因此,当我发送带有此模板的电子邮件时,我想用实际值替换上述常量:
像[[CUSTOMER]]
是“Jemes”等。因为我已经存储了常量,在将此电子邮件模板发送给客户之前,有没有办法知道哪些常量需要替换为哪些值?
我正在使用带有mysql的Symfony2.8
//Controler code Is:
public function sendEmails(){
return $this->render('action_and_message/messageTemplates/emailTemplates /emailTemplate.html.twig', array(
'error' =>"",
'data' =>$getTemplates->getEmailTemplate()
));
}
//My Twig is
{% extends 'emailTemplateLayout.html.twig' %}
{% block content %}
<div style="width:80%; margin: 0 auto; padding: 10px">
{{ data | raw}}
</div>
{% endblock %}
提前致谢
答案 0 :(得分:0)
首先你可能需要创建和使用数据库模板加载器,这里有一个例子(它有点难看,但它有效):
class DBTwigLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface
{
protected $dbh;
protected $table;
public function __construct(\PDO $dbh, $table = 'tmpl_twig')
{
$this->dbh = $dbh;
$this->table = $table;
}
public function getSource($name)
{
if (false === $source = $this->getValue('source', $name)) {
throw new \Twig_Error_Loader(sprintf('Template "%s" does not exist.', $name));
}
return $source;
}
// Twig_ExistsLoaderInterface as of Twig 1.11
public function exists($name)
{
return $name === $this->getValue('name', $name);
}
public function getCacheKey($name)
{
return $name;
}
public function isFresh($name, $time)
{
if (false === $lastModified = $this->getValue('last_modified', $name)) {
return false;
}
return $lastModified <= $time;
}
protected function getValue($column, $name)
{
$sth = $this->dbh->prepare('SELECT ' . $column . ' FROM '.$this->table.' WHERE name = :name');
$sth->execute(array(':name' => (string)$name));
return $sth->fetchColumn();
}
}
之后你可以渲染这样做:
$loader = new DBTwigLoader($dbh);
$twig = new Twig_Environment($loader);
//echo $twig->render('index.twig', array('name' => 'Fabien'));
//or eventually Im doing that (I use just some blocks):
$template = $twig->loadTemplate($messagesPayload['template_name']);
$bodyHtml = $template->renderBlock('bodyHtml', ['recipient' => $recipient]);
Template_name将是&#39; yourtempname&#39;它将在db中的name列下。确保您准备好架构,有关详细信息,请查看:http://twig.sensiolabs.org/doc/recipes.html#using-a-database-to-store-templates
使用上述代码的示例模板:
{% block subject 'Welcome to newsletter, ' ~ data.name %}
{% block bodyHtml %}
<div style="background-color: black; color: lime;">
Here is <strong>free trial of our new soft</strong>.
As of our experience <strong><i>we think that:</i></strong><br/> {{ data.message }}
</div>
{% endblock %}
目前我正在使用一个包裹树枝的服务,只有这个服务从数据库中呈现模板,但我想如果你觉得它更方便,你可以把它连接到你的基础Twig服务。
来自字符串的模板
我不确定您为什么要在模板中使用常量,但最好将它们转换为正确的Twig模板,即[[OWNER_NAME]]到{{OWNER_NAME}},然后Twig将负责替换价值......
class DefaultController extends Controller
{
public function indexAction()
{
//this will create twig template from string and array of data
$template = $this->get('twig')->createTemplate(
"testing {{OWNER_NAME}} tests {{CUSTOMER}}"
);
$templateString = $template->render(
array('OWNER_NAME'=>'Joe', 'CUSTOMER' => 'some customer')
);
// if you're not using it yet, try out dump (VarDumper component),
//included in Symfony standard, it will show in webtoolbar as well
dump($template);
dump($templateString);
//return response if in controller (*using symfony 3.2)
return new Response($templateString);
}
}
如果您不想在数据库中转换模板,那么您可以使用str_replace()函数在传递给crateTemplate函数之前将[[with {{and]]替换为}}。
如果您决定坚持使用[[VAR]]格式,那么pre_re_replace_(或更好)可以为您提供答案,但如果您使用Symfony和twig,我不会明白这一点