Download the PHP package qoliber/tsuku without Composer
On this page you can find all versions of the php package qoliber/tsuku. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download qoliber/tsuku
More information about qoliber/tsuku
Files in qoliber/tsuku
Package tsuku
Short Description A lightweight PHP templating library for transforming data into multiple formats (CSV, XML, TSV, etc.) with XSLT-like simplicity
License MIT
Informations about the package tsuku
Tsuku (つく)
A lightweight PHP templating library for transforming data into ANY text format
Tsuku is a powerful template processing library built with a clean Lexer → Parser → Compiler architecture. Transform your data into CSV, XML, JSON, XSD, or any text format you need using simple, intuitive templates.
Perfect for e-commerce exports, API responses, configuration files, and data transformations.
Features
- 🎯 Any text format: CSV, XML, JSON, YAML, TOML, HTML, Markdown, INI, XSD, or custom formats
- 🔄 Control flow: Loops (
@for), conditionals (@if,@unless,@else), pattern matching (@match) - 🪺 Deep nesting: Unlimited levels of nested directives
- 🎨 Smart object/array access: Automatic getter detection, method calls, property access
- 🔧 Custom functions: Register your own
@function()handlers - 🎭 Widget support: Build Magento-style widgets with custom functions
- 🛠️ Clean architecture: Lexer → Parser → Compiler pipeline (AST-based)
- 🚀 PHP 8.1+: Modern PHP with zero dependencies
- ✅ Production-ready: 196 tests, 423 assertions, 88% mutation score
- 📦 Preserves formatting: Exact whitespace and newline control
- ⚡ Fast: Single-pass compilation, efficient AST walking
- 🔒 Type-safe: Full PHP 8.1+ type hints and strict types
Performance
Tsuku is fast - designed for high-volume data transformations:
| Benchmark | Performance | Throughput |
|---|---|---|
| Simple templates | 0.2ms per render | ~5,000 renders/sec |
| Complex templates | 1.0ms per render | ~1,000 renders/sec |
| 1,000 variables | 1.8ms per render | ~550 renders/sec |
| CSV export (1,000 products) | 3.9ms per export | ~250 exports/sec |
Real-world capacity:
- 250,000+ products/second for CSV exports
- Sub-millisecond rendering for typical templates
- Low memory footprint (~60KB per render)
Run benchmarks yourself:
See benchmarks/ for detailed performance tests.
Requirements
- PHP 8.1 or higher
Installation
Quick Start
Simple Variables
Loops with @for
Conditionals with @if and @else
Smart Object/Array Access
Custom Functions
Streaming Large Datasets
For large catalogs (10k+ rows) where holding the whole rendered output in memory is impractical, use processToStream. It renders the template incrementally — header once, each row in turn, footer once — and writes each piece through a callback as soon as it's ready.
Constraints:
- Template must contain exactly one top-level
@forover the streaming variable. Multiple matches or nesting inside@if/@forthrows. - The streamed iterable is consumed once. Generators cannot be rewound.
- Memory usage is bounded by the size of the largest single piece (header, footer, or one row), regardless of total row count.
Real-World Examples
CSV Export with Escaping
XML Product Catalog
YAML Configuration
HTML Product List with XSS Protection
Template Syntax
Variables
Use {variableName} or {object.property} for dot notation:
Smart Object/Array Access:
For Loops
With key/value (value first, then key):
Conditionals
If/Else:
Unless:
Match (Pattern Matching):
Match with multiple values:
Supported operators: >, <, >=, <=, ==, !=
Built-in Functions
String functions:
Number functions:
Array functions:
Escaping functions:
Date/Utility functions:
Custom Functions
Register your own:
Deep Nesting
Nest directives as deep as you need:
Architecture & Design
Tsuku uses a clean three-stage compiler pipeline inspired by traditional programming language design:
The Pipeline
1. Lexer (Lexical Analyzer)
What it means: "Lexer" comes from "lexical analysis" - breaking text into meaningful chunks
Location: src/Lexer/Lexer.php
The Lexer reads the raw template string character by character and breaks it into tokens (meaningful units):
Why? Makes parsing easier by converting a string into structured chunks.
2. Parser (Syntax Analyzer)
What it means: Builds a tree structure showing how pieces relate to each other
Location: src/Ast/Parser.php
The Parser takes tokens and builds an AST (Abstract Syntax Tree) - a tree structure representing the template's logical structure:
Why? The tree structure makes it easy to handle nesting and execute directives in the correct order.
3. Compiler (Code Generator)
What it means: Walks the tree and generates the final output
Location: src/Compiler/Compiler.php
The Compiler walks the AST tree using the Visitor Pattern and generates the output string:
Why? Clean separation: data lookup, conditionals, loops all handled in one place.
Key Concepts Explained
AST (Abstract Syntax Tree)
- A tree representation of your template structure
- Each node = one piece (text, variable, loop, condition)
- Example:
@if(x)@for(items)...@end@endbecomes a tree with IfNode containing ForNode
Node
- One element in the AST tree
- Types:
TextNode,VariableNode,ForNode,IfNode,FunctionNode, etc. - Each node knows how to compile itself
Token
- Smallest meaningful unit from Lexer
- Like words in a sentence
- Types:
TEXT,VARIABLE,DIRECTIVE_IF,DIRECTIVE_FOR, etc.
Visitor Pattern
- Design pattern where nodes "accept" a visitor (the compiler)
- Allows separating tree structure from processing logic
- Each node has
accept(NodeVisitor $visitor)method
Benefits of This Architecture
✅ Exact whitespace preservation - Lexer captures everything ✅ Proper nesting validation - Parser builds correct tree or throws error ✅ Clean separation of concerns - Each stage has one job ✅ Easy to extend - Add new node types without breaking existing code ✅ Fast execution - Single pass through the tree ✅ Type safety - PHP 8.1+ types ensure correctness
Class Naming Conventions
Tsuku follows industry-standard naming for compiler components:
| Class Name | Purpose | Location |
|---|---|---|
Lexer |
Lexical analyzer - breaks text into tokens | src/Lexer/ |
Token |
One meaningful unit (like a word) | src/Lexer/Token.php |
TokenType |
Enum of all token types | src/Lexer/TokenType.php |
Parser |
Syntax analyzer - builds AST from tokens | src/Ast/Parser.php |
*Node |
AST tree nodes (TextNode, ForNode, etc.) |
src/Ast/ |
NodeVisitor |
Interface for visiting AST nodes | src/Ast/NodeVisitor.php |
Compiler |
Code generator - walks AST to create output | src/Compiler/Compiler.php |
Tsuku |
Main API entry point | src/Tsuku.php |
Naming Philosophy:
- Lexer/Parser/Compiler - Standard compiler pipeline terms
- Node suffix - Indicates AST node type (
TextNode,IfNode) - Registry suffix - Stores and manages items (
FunctionRegistry) - Visitor suffix - Implements visitor pattern (
NodeVisitor) - Exception suffix - Error types (
TsukuException,ParseException)
How It All Works Together
This architecture is the same used by:
- Programming languages (PHP, JavaScript, Python)
- Template engines (Twig, Blade, Smarty)
- Markup processors (Markdown, BBCode)
Further Reading:
Development
License
MIT License - see LICENSE file for details
Credits
Created by qoliber - Like a hummingbird (koliber), swift and precise in data transformation.
Tsuku (つく) means "to create" or "to make" in Japanese, reflecting the library's purpose of creating text output from data.