2016-12-06 6 views
0

こんにちは実際の値と定数を置き換えます。次のように:私はデシベルでテンプレートストアを持ってsymfonyの

<p>Dear [[OWNER_NAME]],</p> 

<p><br /> 
We are thanks put&nbsp;[[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> 

をだから私は、このテンプレートを使用して電子メールを送信していたとき、私は実際の値と上記の定数を置き換えたい:

Like [[CUSTOMER]]は "Jemes"などです。既に定数が保存されているので、この電子メールテンプレートを顧客に送信する前にどの定数をどの値に置き換える必要があるかを知る方法はありますか?

は、私が事前に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 %} 

感謝をSymfony2.8を使用しています

答えて

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は '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を使っているなら、

+0

実際には、私は完全なテンプレートをdbに格納していません。私はdbカラムにコンテンツ部分だけを保存しています。電子メールを送信している間に実行時にこれらの変数を値で置き換えるだけの他の方法があります。 –

+0

私の編集を見てください –

関連する問題