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

Volt Compiler - Using stand alone

Volt Compiler is useful for templates outside of views. The "Stand-alone component" section of https://docs.phalcon.io/en/3.0.0/reference/volt.html is a start. I eventually made the compiler work. Perhaps the Compiler could have a page by itself.

My use has some parallels to the discussion in https://forum.phalcon.io/discussion/15855/render-html-email-template. I created a generic direct use of the volt compiler in a controller to test the concept. It works when I use files. It did not work when I tried to use strings. I might revisit that.

The first requirement is to isolate the processing of templates because they may be user summitted. Using the compiler direct does give me control of the variables presented. What I need next are things like the site name and the URL for references to pages on the site. I need to selectively add just a few items but from the Phalcon code to allow for any processing within Phalcon code before reaching the template code.

The next option is files/string. For a one off template, string processing avoids the overheads of file processing. For repeat use, I will use files to cache the compiled template. The string version would be used when a content editor is editing a template and wants to see the result. The repeat version would be after the edited template is saved. The compiled template could be retained until the template is edited and saved.

Has anyone used Volt Compiler like this? Any tips? Would a separate Volt Compiler page help?



9.7k
Accepted
answer

I will mark this as solved because it works for me using the files approach. $this->volt->compileFile($this->template_path, $this->compiled_path);

I found the "compile always" option to process frequently changing templates. I may also look at deleting the template and compiled file after use when I want to process a small batch withthe same template. $this->volt->setOption('compileAlways', true);