How to render Volt template outside application

Hi all,

My application has a base.phtml file that is the basis for all pages. I'm writing some documentation and I'd like to re-use that template. The PHP file that will be used to display the documentation is outside the main /app directory. I think I could probably include the bootstrap.php file and get things working, but that seems like an awful lot of work and resources simply to render a template.

I've tried:

$View = new \Phalcon\Mvc\View\Simple();
$View->setDI(new Phalcon\DI\FactoryDefault());
$View->registerEngines(['.phtml'=>Phalcon\Mvc\View\Engine\Volt::class]);
echo $View->render('../../../app/views/partials/base.phtml');

but then I get this error:

Warning: Phalcon\Mvc\View\Engine\Volt\Compiler::compileFile(../../../app/views/partials/base.phtml.php): failed to open stream: Permission denied in /var/www/htdocs/point/public/documentation/developer/parse.php on line 20

I tried using the raw Volt compiler, but it just generates the intermediate PHP code which I then need to eval(), which I am loath to do.

Isn't there a simple way to just say "Here's my template file X, render it and give me the resulting HTML output"? I suppose the raw compiler is pretty close, but I'd like to be cleaner if possible.

Hi @Dylan do you check this stand-alone content docs for view or Volt?

edited Jan '18

Hi @Dylan do you check this stand-alone content docs for view or Volt?

Thanks for that - for some reason I hadn't seen the stand-alone link for the view. However, with that technique, I don't get any output. I'm sure I'm doing something wrong, but I'm not getting any errors:

$View = new Phalcon\Mvc\View();

$View->setViewsDir('..../../app/views/');
$View->setVar('someProducts', 'test');

$View->start();
$View->render('partials', 'base');
$View->finish();

echo $View->getContent();
edited Jan '18

It's easier to use Simple really. If you dont have anything then most likely this view dir or this view doesn't exist(most likely just wrong path). Just do file_exists("..../../app/views/partials/base.phtml")

Warning: Phalcon\Mvc\View\Engine\Volt\Compiler::compileFile(../../../app/views/partials/base.phtml.php): failed to open stream: Permission denied in /var/www/htdocs/point/public/documentation/developer/parse.php on line 20

Why just not fix permissions? It's about your file permissions. Just fix them and it will work fine :)

base.phtml.php doesn't exist. My template file is base.phtml. What am I missing?

edited Jan '18

Im not sure but isn't this about trying to write cache to php file? And it doesn't have proper file permissions to save to this path? Base.phtml is compiled into base.phtml.php which then is included.

This is my new code:

$views_dir = realpath('../../../app/views/').'/';
$DI = new Phalcon\DI\FactoryDefault();
$View = new \Phalcon\Mvc\View\Simple();
$View->setDI($DI);
$View->registerEngines(['.phtml'=>function($View,$DI,$views_dir){
    $Volt = new \Phalcon\Mvc\View\Engine\Volt($View,$DI);
    $Volt->setOptions(['compiledPath'=>$views_dir.'compiled/',
                       'compiledSeparator' => '__']);

    return $Volt;
}]);

$View->setViewsDir($views_dir);
echo $View->render('partials/base');

Which is generating these errors:

Warning:  Phalcon\Mvc\View\Engine\Volt\Compiler::compileFile(compiled/__var__www__htdocs__point__app__views__partials__base.phtml.php): failed to open stream: No such file or directory in /var/www/htdocs/point/public/documentation/developer/parse.php on line 28

Fatal error:  Uncaught Phalcon\Mvc\View\Engine\Volt\Exception: Volt directory can't be written in phalcon/mvc/view/engine/volt/compiler.zep:2364
Stack trace:
#0 [internal function]: Phalcon\Mvc\View\Engine\Volt\Compiler->compileFile('/var/www/htdocs...', 'compiled/__var_...', false)
#1 [internal function]: Phalcon\Mvc\View\Engine\Volt\Compiler->compile('/var/www/htdocs...')
#2 [internal function]: Phalcon\Mvc\View\Engine\Volt->render('/var/www/htdocs...', NULL, true)
#3 [internal function]: Phalcon\Mvc\View\Simple->_internalRender('partials/base', NULL)
#4 /var/www/htdocs/point/public/documentation/developer/parse.php(28): Phalcon\Mvc\View\Simple->render('partials/base')
#5 {main}
  thrown in phalcon/mvc/view/engine/volt/compiler.zep on line 2364

I don't know where the compiler is looking - I've re-built the View just like my bootstrap file, or so I believe. I'm sure I'm missing something - but it seems like this is too much work simply to get Phalcon to output a template.

I've spent too much time on this. I just included my Bootstrap file and used the SimpleView component I have set up there. Thanks for the assistance.



37.6k

Hi,

Fatal error: Uncaught Phalcon\Mvc\View\Engine\Volt\Exception: Volt directory can't be written

you have to set write access on $views_dir.'compiled/' to the user who runs the webserver (www-data for apache2 in most linux distros)