4

後で送信するために電子メールテンプレートを変数でレンダリングする必要があり (.phtml ファイルに保存されます)、これを処理するための特別なクラスを実装したくありません。

コントローラー アクション ビューではなくカスタム ビューをレンダリングすることはできますか?

次のコードを試しましたが、NULLを出力します:((

// Controller context
$view = new Phalcon\Mvc\View();
$view->setViewsDir('app/views/');
$view->setVar('var1', 'var2');
// Setting some vars...
$view->start();
$view->partial($emailTemplatePath);
$view->finish();
$result = $view->getContent();
var_dump($result); // Gives null
4

6 に答える 6

4

Nikolaos による応答に加えて、$view->getRender() を使用して、出力を返す単一のビューをレンダリングできます。

$view->setViewsDir('apps/views/');
echo $view->getRender('partials', 'test'); // get apps/views/partials/test.phtml
于 2013-01-15T01:22:39.637 に答える
2

のパスを確認する必要があります$emailTemplatePath。正しいファイルを指す必要があります。

// points to app/views/partials/email.phtml
$view->partial('partials/email');

Volt を使用していて、それをエンジンとして登録している場合、ファイルは次のようにする必要があります。

// app/views/partials/email.volt
于 2013-01-14T22:50:31.140 に答える
1

$view->render('partials/email')部分メソッドを呼び出す代わりに使用します。

于 2014-01-02T06:44:27.220 に答える
1

ビューをレンダリングして変数として返す最も簡単な方法は、Phalcon\Mvc\View\Simple クラスを使用することです。コントローラーで、Simple ビュー クラスの新しいインスタンスを宣言し、レンダリング エンジンをアタッチします。次に、render() メソッドを使用してビュー ファイルを選択し、変数を渡すことができます。

// create a simple view to help render sections of the page
$simple_view = new \Phalcon\Mvc\View\Simple();
$simple_view->setViewsDir( __DIR__ . '/../views/' );
$simple_view->setDI( $this->di );
$simple_view->registerEngines(array(
    '.volt' => 'Phalcon\Mvc\View\Engine\Volt'
));

// use the simple view to generate one or more widgets
$widget_html = array();
$widget_objects = $widget_search->getWidgetObjects();
forEach( $widget_objects as $widget ){
    $widget_html[] = $simple_view->render('index/widgetview',array('widget'=>$widget));
}

// pass the html snippets as a variable into your regular view
$this->view->setVar('widget_html',$widget_html);
于 2014-10-28T22:46:00.120 に答える
1

私は電子メールと 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');

このように個別のビュー エンジンをコンポーネントに配置する必要はありません。そのようにメモリを消費する可能性がありますが、プロセス全体が表示されます。それが理にかなっていることを願っています:)

于 2014-08-14T19:25:22.330 に答える
0

私は通常、Volt エンジンを使用します。簡単な方法は、次のように DI コンテナーでビューを再定義することです。

$view = $this->view;

$content = $view->getRender('mail', 'show',
  array(
    "var1" => "some value 1",
    "var2" => "some value 2"
  ),
  function($view) {
      $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT);
  }
);

echo $content;
于 2015-02-11T10:00:22.583 に答える