Skip to main content
All Wik/Lex exceptions extend Wik\Lexer\Exceptions\LexException, which itself extends \RuntimeException. You can catch any Lex exception by catching LexException, or catch individual types for more precise error handling.
use Wik\Lexer\Exceptions\LexException;

try {
    echo $lexer->render('home', $data);
} catch (LexException $e) {
    // Catches any Lex exception
    error_log($e->getMessage());
}
In development mode, the Chrome DevTools error overlay intercepts TemplateSyntaxException and displays the file, line, column, and source snippet with an Open in VS Code button. See the DevTools extension guide for setup instructions.

Exception reference

Full class name: Wik\Lexer\Exceptions\TemplateSyntaxExceptionThrown when a template contains a syntax or structural error detected at compile time — during lexing, parsing, or AST validation.Data it carries
PropertyTypeDescription
$templateFilestringAbsolute path to the template file
$templateLineintLine number where the error occurred
$templateColumnintColumn number where the error occurred
$snippetstringFormatted source snippet (±2 lines around the error)
When it is thrown
  • Parse or compile errors with a precise source location
  • Unclosed blocks (e.g. #if with no #endif)
  • Mismatched component closing tags
  • Duplicate #section definitions
  • Sandbox violations (forbidden expression or function)
  • Invalid PHP expressions in {{ }} or {!! !!}
Example
use Wik\Lexer\Exceptions\TemplateSyntaxException;

try {
    echo $lexer->render('home', $data);
} catch (TemplateSyntaxException $e) {
    echo $e->getMessage();      // Full message with location and snippet
    echo $e->templateFile;      // e.g. /var/www/views/home.lex
    echo $e->templateLine;      // e.g. 12
    echo $e->templateColumn;    // e.g. 5
    echo $e->snippet;           // Formatted source context
}
Full class name: Wik\Lexer\Exceptions\TemplateRuntimeExceptionThrown when an error occurs while executing a compiled template — that is, after compilation has succeeded and the compiled PHP is actually running.Data it carries
PropertyTypeDescription
$templateFilestringPath to the template that triggered the error
$templateLineintLine number in the source template (when available)
When it is thrown
  • An infinite layout loop is detected (a layout template renders itself)
  • Component recursion exceeds the configured limit
  • {!! ... !!} raw echo is used in sandbox mode with rawEcho disabled
  • A PHP error occurs inside a compiled template at runtime
Example
use Wik\Lexer\Exceptions\TemplateRuntimeException;

try {
    echo $lexer->render('home', $data);
} catch (TemplateRuntimeException $e) {
    echo $e->getMessage();   // e.g. "Component recursion limit (50) exceeded for component 'Tree'."
    echo $e->templateFile;   // Template that was being rendered
}
Full class name: Wik\Lexer\Exceptions\ViewExceptionThrown when the view engine cannot locate a template or component file, or when no view paths or cache directory have been configured.Data it carriesInherits the standard RuntimeException $message. No additional properties.When it is thrown
  • A template name cannot be resolved to a .lex file in any configured view path
  • A component tag cannot be resolved to a .lex file in any components/ subdirectory
  • No cache directory has been configured before rendering
  • No view paths have been configured before rendering
Example
use Wik\Lexer\Exceptions\ViewException;

try {
    echo $lexer->render('pages.missing', $data);
} catch (ViewException $e) {
    // "Template 'pages.missing' not found. Searched in: ['views', 'resources/views']."
    echo $e->getMessage();
}
Full class name: Wik\Lexer\Exceptions\LexerExceptionThrown by the tokenizer when it encounters an unterminated expression or tag during the lexing pass.Data it carriesInherits the standard RuntimeException $message. The line number where the unterminated expression started is included in the message.When it is thrown
  • An echo expression {{ ... }} is opened but never closed
  • A raw echo expression {!! ... !!} is opened but never closed
  • A component opening tag <ComponentName is not terminated
Example
use Wik\Lexer\Exceptions\LexerException;

try {
    echo $lexer->render('broken', $data);
} catch (LexerException $e) {
    // "Unterminated echo expression {{ ... }} starting on line 8."
    echo $e->getMessage();
}
Full class name: Wik\Lexer\Exceptions\ParseExceptionThrown by the parser when it encounters structural problems in the token stream — such as mismatched blocks or directives it does not recognise.Data it carriesInherits the standard RuntimeException $message. The relevant line number is embedded in the message.When it is thrown
  • A closing directive appears with no matching opening block (e.g. #endif with no #if)
  • A closing component tag has no matching opening tag
  • A component opening tag and its closing tag have different names
  • A block is still open when a parent block is closed (nested block must close first)
  • An unknown directive name is encountered and has not been registered via $lexer->directive()
Example
use Wik\Lexer\Exceptions\ParseException;

try {
    echo $lexer->render('page', $data);
} catch (ParseException $e) {
    // "Unknown directive #highlight on line 14. Register it via $lexer->directive()."
    echo $e->getMessage();
}
Full class name: Wik\Lexer\Exceptions\CompilerExceptionThrown when the compiler cannot write to the cache directory or cannot complete compilation of a template.Data it carriesInherits the standard RuntimeException $message. No additional properties.When it is thrown
  • The cache directory does not exist and cannot be created, or is not writable
  • Compilation of a specific template fails (message includes the template name and reason)
Example
use Wik\Lexer\Exceptions\CompilerException;

try {
    echo $lexer->render('home', $data);
} catch (CompilerException $e) {
    // "Cache directory '.lexer/compiled' is not writable or could not be created."
    echo $e->getMessage();
}

Exception hierarchy

\RuntimeException
└── Wik\Lexer\Exceptions\LexException
    ├── TemplateSyntaxException
    ├── TemplateRuntimeException
    ├── ViewException
    ├── LexerException
    ├── ParseException
    └── CompilerException
Catch TemplateSyntaxException, LexerException, and ParseException together in your error handler to cover all compile-time failures. Catch TemplateRuntimeException separately if you need to handle render-time failures differently.

Build docs developers (and LLMs) love