まずあなたが作成し、データベースのテンプレートローダを使用する必要があるかもしれませんが、ここでは一例です(そのビット醜いが、それは動作します) :あなたがレンダリングすることができます。その後
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は 'yourtempname'になります。これはdbのnameカラムになります。ただ小枝とをラップサービスを利用して瞬間イムについて
{% 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 %}
:上記のコードで動作しますhttp://twig.sensiolabs.org/doc/recipes.html#using-a-database-to-store-templates
サンプルテンプレート:それを見て、あなたがより多くの細部のビットのために、スキーマを準備ことを確認してくださいこのサービスはデータベースからテンプレートをレンダリングしますが、もっと便利であれば、基本のTwigサービスに接続できると思います。 {{OWNER_NAMEに[[OWNER_NAME]]私はなぜあなたはテンプレートの定数を使用しているわからないんだけど、適切な小枝テンプレートすなわちに変換する方が良いと思います文字列
から
テンプレート}}その後、小枝は値を置き換えるの世話をする...
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関数に渡す直前。
もしあなたが[[VAR]]フォーマットに固執するならば、str_replace()またはpreg_replace_(all)の方が答えがいいかもしれませんが、Symfonyとtwigを使っているなら、
実際には、私は完全なテンプレートをdbに格納していません。私はdbカラムにコンテンツ部分だけを保存しています。電子メールを送信している間に実行時にこれらの変数を値で置き換えるだけの他の方法があります。 –
私の編集を見てください –