We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Compiled Path in volt is not working

my view setup is undergiven. The compiled path is not working, i.e. the compiled file filename.volt.php is not saving in app/cache/volt but saving in the same directory as of the view file.

can anyone help?

$di->set('view', function() use ($config, $di) {

    $view = new View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array('.volt' => function ($view, $di) use ($config) {

        $volt = new VoltEngine($view, $di);

        $volt->setOptions(array(
            'compiledPath' =>  APP_PATH . '/cache/volt/',
            'compiledSeparator' => '_',
            $volt->getCompiler()->addFilter(
                'repspace',
                function($resolvedArgs, $exprArgs) {
                    return ' str_replace(" ", "_", '.$resolvedArgs.')';
                }
            ),
            $volt->getCompiler()->addFunction("echo", function($args, $expr) {
                $args = explode(',', $args);
                $var = $args[0];
                $default = isset($args[1]) ? $args[1] : '';
                return "isset($var) ? $var : $default";
            })
        ));

        return $volt;
    }));

    // Set the translator service as a shared variable on the view.
    $view->setVar('t', $di->getTranslator());

    return $view;
}, true);


85.5k

perhaps you have multi module app and you create the view service every time ? cuz i have pretty much the same code and its working

Hey, you guessed it right. I have total 3 sub modules in it.

main_repo
    \----> app/controllers (sub module i.e. another git repo)
    \----> app/views (sub module i.e. another git repo)
    \----> public (sub module i.e. another git repo)

I didn't get the meaning of "creating the view service everytime". Can you please tell me, what should be done in this case in order to run this app. Thanks for your help.



85.5k
edited Nov '16

nah those are not modules.. i was wrong ...

you dont have set('view'... anywhere else in the code right ?

show me the simpliest code of any controller action where this doesnt work

and show me how you define APP_PATH

edited Nov '16

oh, okay.

Yes, I don't have set('view' in the code anywhere else; just in service.php file.

Actually the code in controller and volt is working fine, the only thing is the volt compiled path are not getting save in the right directory. i have app/cache in gitignore, which is why it is the best dir for me to save the volt compiled files, but now they are getting save in the same directory because of which i will not be able to push the changes.

class AboutController extends ControllerBase
{
    public function indexAction()
    {
        $this->view->setTemplateBefore('company');
    }
}

about/index.volt file just have html.
/var/www/html/whitespace/book24_final$ git status
On branch development
Your branch is up-to-date with 'origin/development'.
nothing to commit, working directory clean

/var/www/html/whitespace/book24_final$ cd app/views

/var/www/html/whitespace/book24_final/app/views$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

/var/www/html/whitespace/book24_final/app/views$ cd ../..

# then i open the url in the browser and the page opens fine, after that i check the status of my repo

/var/www/html/whitespace/book24_final$ git status
On branch development
Your branch is up-to-date with 'origin/development'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)

    modified:   app/views (untracked content)

no changes added to commit (use "git add" and/or "git commit -a")

/var/www/html/whitespace/book24_final$ cd app/views

/var/www/html/whitespace/book24_final/app/views$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    about/index.volt.php
    index.volt.php
    layouts/company.volt.php

nothing added to commit but untracked files present (use "git add" to track)

/var/www/html/whitespace/book24_final/app/views$ 

This is my definetion of APP_PATH in main_repo/public/index.php

define('BASE_DIR', dirname(__DIR__));
define('APP_PATH', BASE_DIR . '/app');

the only way around I can found is to put */*.volt.php in .gitignore but this doesn't feel right to me.



43.9k

Hi,

are you sure that APP_PATH is not an empty string ?

Hey, yup, its correct. I just echoed the APP_PATH and got this /var/www/html/whitespace/book24_final/app.



43.9k

Strange, because as is,


define('BASE_DIR', dirname(__DIR__));  // should give you /var/www/html/whitespace/book24_final/public

define('APP_PATH', BASE_DIR . '/app'); // should give you /var/www/html/whitespace/book24_final/public/app

so I don't understand how echo APP_PATH outputs /var/www/html/whitespace/book24_final/app

otherwise, your volt definition looks correct. I can't tell you why compiled volt files are located in the views directory ....

Actually, __DIR__ returns the path to the current directory and dirname() returns the path to the parent directory, that is why using dirname(__DIR__) is giving the above results. https://php.net/manual/en/function.dirname.php

Well, thanks for your efforts, I really appreciate it; and as soon i get the solution i will be posting it here.



43.9k

you're right. My mistake

Actually, __DIR__ returns the path to the current directory and dirname() returns the path to the parent directory, that is why using dirname(__DIR__) is giving the above results. https://php.net/manual/en/function.dirname.php

Well, thanks for your efforts, I really appreciate it; and as soon i get the solution i will be posting it here.

I had a very similar problem just now and I fixed it by pulling volt in it's own service and only calling $volt->getCompiler(); after setting the options. Hope this helps.