Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to render custom view (just custom .phtml) template with Phalcon\Mvc\View?

Tags:

phalcon

I need to render email templates in variable to send them later (which are stored in .phtml files), and i really don't want to implement my special class for handling this.

Is it possible to render not controller action view, but custom one?

I tried following code, but it outputs 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
like image 588
avasin Avatar asked Jan 15 '23 12:01

avasin


2 Answers

In addition to the response by Nikolaos, you can use $view->getRender() to render a single view returning its output.

$view->setViewsDir('apps/views/');
echo $view->getRender('partials', 'test'); // get apps/views/partials/test.phtml
like image 190
twistedxtra Avatar answered May 06 '23 20:05

twistedxtra


You need to check the path of the $emailTemplatePath. It should point to the correct file i.e.

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

If you are using Volt and have registered that as your engine, then your file will need to be:

// app/views/partials/email.volt
like image 24
Nikolaos Dimopoulos Avatar answered May 06 '23 21:05

Nikolaos Dimopoulos