php-liquid
A PHP port of Shopify's Ruby Liquid template engine with a safe, human-friendly syntax.
Repository Health
Technical Analysis
php-liquid is a PHP implementation of the Liquid template engine originally written in Ruby by Tobias Lutke for Shopify. It offers a readable, human-friendly templating syntax that works in any document type and never allows embedding PHP code, making templates safe to expose to end users.
Liquid separates parsing and rendering into distinct stages for better performance, follows a clean object-oriented design, and is easy to extend with your own tags and filters. It maintains markup compatibility with the Ruby engine, so templates can be shared across both.
What You Get
- The
Liquid\Templateclass with separateparse()andrender()stages - The full Liquid tag set (
if,for,assign,capture, and more) and standard filters - A sandboxed syntax that cannot execute arbitrary PHP
- An extension API for registering custom tags and filters
- Markup compatibility with Shopify’s Ruby Liquid engine
Common Use Cases
- Letting end users write safe templates for emails or pages without code injection risk
- Rendering CMS or newsletter content from stored template strings
- Sharing template markup between Ruby and PHP applications
- Building themeable output where designers edit templates without touching PHP
Under The Hood
Architecture
The entry point is Liquid\Template, which uses a lexer/tokenizer to split source into tags and variables, then builds a document tree of AbstractBlock/AbstractTag nodes during parse(). Rendering walks that tree against a Context object that resolves variables and applies filters. Because parsing and rendering are separate, a parsed template can be cached and re-rendered cheaply. Tags and filters are resolved through a registry, keeping the core small and extensible.
Tech Stack
Pure PHP with no runtime framework dependency, distributed via Composer as liquid/liquid. The library uses standard OOP class hierarchies for tags, blocks, and filters; tests run under PHPUnit with coverage reporting through Coveralls and CI via GitHub Actions.
Code Quality
The project advertises being fully unit-tested and stable for large projects, backed by a long release history (50+ releases since 2014) and continuous integration. The clean OO separation of lexer, nodes, context, and filters keeps responsibilities isolated, and the codebase avoids the mixed procedural/OO style common in older PHP template engines.
API Design
The public surface is deliberately tiny: construct a Template, call parse() with the source, then render() with an associative array of data. Extending with custom tags and filters follows documented base classes, and the Liquid syntax itself is well established and widely documented. The main learning curve is the Liquid language semantics rather than the PHP API.