Skip to main content
Wik/Lex is a production-grade PHP template engine that compiles .lex templates through a full AST pipeline — Lexer → Parser → Validator → Optimizer → PHP file. It works with any PHP project and requires no framework.

Installation

Install via Composer and get up and running in minutes

Quick Start

Render your first template with the fluent API or config file

Template Syntax

Learn the full .lex syntax: echo, directives, loops, and includes

Components

Build reusable UI with PascalCase component tags and named slots

Layout Inheritance

Compose pages from layouts with sections, yields, and stacks

Sandbox Mode

Safely render user-submitted templates with expression whitelisting

How it works

1

Install the package

Add Wik/Lex to your project with Composer:
composer require wik/lexer
2

Create a config file

Add lex.config.json to your project root:
lex.config.json
{
  "viewPaths": ["views", "resources/views"],
  "production": false,
  "sandbox": false
}
3

Write a template

Create a .lex file in your views directory:
views/home.lex
<h1>{{ $title }}</h1>

#isset($user)
    <p>Welcome back, {{ $user->name }}!</p>
#endisset
4

Render it

Call render() from your application code:
use Wik\Lexer\Lexer;

$lexer = Lexer::fromConfig();
echo $lexer->render('home', ['title' => 'Hello World', 'user' => $user]);

Key features

AST compilation pipeline

Templates are compiled once through a full parse tree — never evaluated with regex at runtime.

Dependency graph cache

When a shared partial or layout changes, every template that imports it is automatically recompiled.

Custom directives

Register any PHP callable as a template directive, resolved at compile time.

CLI tooling

Precompile templates, clear caches, run benchmarks, and validate syntax from the command line.

Build docs developers (and LLMs) love