wol-soft/php-micro-template

Template Syntax checker

scramatte opened this issue · 1 comments

Hello,

It would be nice to have some method to check syntax of the template.
I use your engine with Laravel/Nova application and I would like to have a validation function to check syntax before store it. into database.

I got stupid issues with foreach because I was trying to use PHP syntax instead of the correct one.

Thank you

Hi @scramatte,

by using the error handler for unresolved variables from version 1.6.0 you can easily implement a syntax checker yourself with something like the code snippet below:

function checkTemplateSyntax(string $template): bool {
    $render = (new Render())->onResolveError(fn () => '');
    
    try {
        $render->renderTemplateString($template);

        return true;
    } catch (Exception $e) {
        return false;
    }
}

Cheers