We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Volt test syntax

Is there any way to test the syntax of a volt file with PHP?


What do you mean by "test"? Make unit tests, or just check if it's working?

I am making a tool where the user will edit the views directly in the browser, I need to know if the volt code entered is valid before saving the file.

Sorry for the language barrier

Your English is pretty much perfect - no barrier. I just wasn't sure what you meant.

There's no way to test if the volt file will generate the HTML the author expects. But there is a way to test if the file will compile.

There may be a simpler approach, but how I would do it is with a SimpleView service. When the user saves the view, save the Volt syntax to a temporary file. Then, try to render it with SimpleView. If an exception is thrown, then the syntax was invalid.

I don't know if SimpleView will generate notices and errors in addition to Exceptions, but if it does, you can use set_error_handler() to convert notices & errors to Exceptions - like the first example here: https://www.php.net/manual/en/class.errorexception.php