Hi I have a template store in db: as following:
<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>
So when I am sending the email with this template I want to replace the above constants with actual values:
Like [[CUSTOMER]]
is "Jemes" etc. Because I have already stored the constants, is there any way to know which constants need to be replaced with which values before sending this email template to customer ?
I am using Symfony2.8 with mysql
//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 %}
Thanks in advance
First you might need to create and use database template loader, here's an example (its a bit ugly but it works):
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();
}
}
After that you can render do this:
$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 will be 'yourtempname' which will be under name column in db. Make sure you prepare schema, for a bit of more details have a look at that: http://twig.sensiolabs.org/doc/recipes.html#using-a-database-to-store-templates
Sample template that works with code above:
{% 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 %}
For the moment Im using a service that wraps twig and just this service render templates from database, but Im I think you can connect it to your base Twig service if you find it more convenient.
Template from string
I'm not sure why you're using constants in templates, but it'd be better to convert them to proper Twig templates i.e. [[OWNER_NAME]] to {{ OWNER_NAME }} and then Twig will take care of replacing values...
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);
}
}
If you dont want to convert templates in database then you can use str_replace() function to replace [[ with {{ and ]] with }} just before passing to crateTemplate function.
If you decide to stick with [[VAR]] format then str_replace() or better by preg_replace_(all) can be answers for you, but I dont see point in doing that if you're using Symfony and twig.