私は電子メールと pdf テンプレートを使用するプロジェクトを持っています。私がしたことは、レンダリングをすべてコンポーネント内で行うことでした。
まず、私のフォルダー構造には、キャッシュ、コンポーネント、およびビューのディレクトリが含まれています (関連するものだけをここに記載します)。PDF ではなく電子メールの設定を見てみましょう。こちらの方が状況に関連しています。
/app
/cache
/email
/components
/views
/email
/elements
もちろん、パブリック、コントローラーなどがありますが、これについては考えないようにしましょう。
私は Swift メーラーを使用していますが、これも同じように使用できることを願っています。/app/components/Swift.php には、this->init_template_engine();を呼び出す __construct があります。
/**
* Create a volt templating engine for generating html
*/
private function init_template_engine() {
$this->_template = new \Phalcon\Mvc\View\Simple();
$di = new \Phalcon\DI\FactoryDefault();
$this->_template->setDI($di);
$this->_template->registerEngines([
'.volt' => function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions([
'compiledPath' => APP_DIR."cache".DS."email".DS, // render cache in /app/cache/email/
'compiledSeparator' => '_'
]);
return $volt;
// or use ".phtml" => 'Phalcon\Mvc\View\Engine\Php' if you want,
// both will accept PHP code if ya don't fancy it being a 100% volt.
},
]);
// tell it where your templates are
$this->_template->setViewsDir(APP_DIR.'views'.DS.'email'.DS);
return $this->_template;
}
上記の定数 (APP_DIR など) は、ブートストラップで既に作成したものであり、ディレクトリへのフル パスを格納するだけです。
$_template 変数にテンプレート エンジンが設定されると、それを使用してテンプレートをレンダリングできます。
/**
* Returns HTML via Phalcon's volt engine.
* @param string $template_name
* @param array $data
*/
private function render_template($template_name = null, $data = null) {
// Check we have some data.
if (empty($data)) {
return false; // or set some default data maybe?
}
// Use the template name given to render the file in views/email
if(is_object($this->_template) && !empty($template_name)) {
return $this->_template->render($template_name, ['data' => $data]);
}
return false;
}
サンプルの volt 電子メール テンプレートは次のようになります。
{{ partial('elements/email_head') }}
<h2>Your Order has been dispatched</h2>
<p>Dear {{ data.name }}</p>
<p>Your order with ACME has now been dispatched and should be with you within a few days.</p>
<p>Do not hesitate to contact us should you have any questions when your waste of money arrives.</p>
<p>Thank you for choosing ACME Inc.</p>
{{ partial('elements/email_foot') }}
あとは、html を取得して、swiftmailer の setBody メソッドを使用するだけで完了です。
->setBody($this->render_template($template, $data), 'text/html');
このように個別のビュー エンジンをコンポーネントに配置する必要はありません。そのようにメモリを消費する可能性がありますが、プロセス全体が表示されます。それが理にかなっていることを願っています:)