Skip to main content
Wik/Lex is a modern, production-grade AST-based view engine for PHP 8.1+. It compiles .lex template files into cached PHP files through a multi-stage pipeline, and works with any PHP project — no framework required.

How it works

Every .lex template you write passes through five sequential stages before it reaches the browser:
Template source (.lex)


    Lexer          character-by-character tokenizer
        │  Token[]

    Parser         explicit stack → nested AST
        │  Node[]

    Validator      sandbox enforcement, structural checks
        │  Node[]

    Optimizer      merge adjacent TextNodes, remove empty nodes
        │  Node[]

  Code generation  Node::compile() → PHP source string
        │  string

    FileCache      atomic write → .lexer/compiled/{md5}.php
        │  path

    include()      executed in isolated scope
In development mode Lex watches source file modification times and recompiles automatically on the next render() call. In production mode all source I/O is skipped and templates are served directly from a precompiled index — zero recompilation per request.

Key features

Layout inheritance

Compose pages from reusable layouts using #extends, #section, #yield, and #parent. Push content into named stacks with #push / #stack.

Components

Use PascalCase tags as self-contained components with named slots, dynamic props (:prop="$expr"), and optional PHP class mounting via mount().

Dependency graph cache

Lex records every #extends, #include, and component dependency at compile time. When a shared partial changes, every template that depends on it is automatically invalidated and recompiled on the next request.

Sandbox mode

Restrict what template authors can do with an expression whitelist, raw-echo control, and 50+ always-blocked functions (eval, exec, system, and more). Useful for user-submitted templates.

CLI (`bin/lex`)

A command-line tool is registered at bin/lex after installation. Use it to warm up the template cache before deployment or to initialise a new lex.config.json.

Chrome DevTools extension

In development mode every render() call injects a JSON debug payload. The Lex DevTools Chrome extension surfaces a component tree, section inspector, cache viewer, error overlay with “Open in VS Code”, and a render-time timeline.

Requirements

PHP^8.1
Extensionsmbstring (recommended), igbinary (optional — faster AST cache serialisation)
Dependenciesphpunit/phpunit, psr/http-server-middleware
The igbinary extension is entirely optional. When present, Lex uses it for faster AST snapshot serialisation in .lexer/ast/. Without it, standard PHP serialisation is used automatically.

Installation

Install via Composer and set up lex.config.json for your project.

Quick Start

Render your first template in minutes using the config-file or fluent API.

Build docs developers (and LLMs) love