Skip to content

TsukuTransform Data Into Any Format

A lightweight PHP templating library built with clean Lexer → Parser → Compiler architecture

Tsuku Logo

Quick Example ​

Transform your data into any format with simple, intuitive templates:

php
use Qoliber\Tsuku\Tsuku;

$tsuku = new Tsuku();

$template = 'SKU,Name,Price,Stock
@for(products as product)
@csv(product.sku),@csv(product.name),@number(product.price, 2, ".", ""),{product.stock}
@end';

$data = [
    'products' => [
        ['sku' => 'WID-001', 'name' => 'Widget', 'price' => 29.99, 'stock' => '100'],
        ['sku' => 'GAD-002', 'name' => 'Gadget, Premium', 'price' => 1299.50, 'stock' => '50'],
    ],
];

echo $tsuku->process($template, $data);

Output:

csv
SKU,Name,Price,Stock
WID-001,Widget,29.99,100
GAD-002,"Gadget, Premium",1299.50,50

@csv() quotes the name that contains a comma, and @number(price, 2, ".", "") keeps the thousands separator out of the price column.

What's New in Tsuku 1.4.0 ​

  • Real conditions - and / &&, or / ||, not / !, parentheses and in / not in in @if, @unless, @elseif and @?{...} (Conditionals)
  • @elseif branches with a single @end
  • Whitespace control - lines that hold only directives vanish, so templates can be indented freely (Whitespace Control)
  • Custom block directives - registerDirective() now works: @cdata ... @end, @wrap("[", "]") ... @end (Custom Directives)
  • @@ prints a literal @ - user@@example.com, @@media (...) (Escapes & Special Characters)
  • Clear errors - malformed conditions throw a ParseException with line and column instead of rendering something wrong
  • Since 1.3.0: streaming with processToStream() for feeds with hundreds of thousands of rows (Streaming)

1.4.0 changes some output compared to 1.3.0 (mostly whitespace around directives) - read Upgrading from 1.3 before you update, and the Changelog for everything else.

1.4.1 is a bug-fix release: warnings of a per-call strictness mode can be read, functions that throw inside conditions follow the strictness mode, @substr() and friends accept numbers from the data, \" is a quote inside a quoted string, uncallable function names are refused when registered, and registerFormatter() works. See Upgrading to 1.4.1.

1.4.2 fixes three more: @csv() quotes a value holding a carriage return, a quoted function argument or @?{...} value may span lines, and WARNING mode keeps at most 1,000 warnings per call so streaming memory stays bounded (getWarningCount() has the total). See Upgrading to 1.4.2.

Why Tsuku? ​

Perfect for E-commerce & Data Exports ​

Tsuku excels at transforming structured data into various text formats - CSV exports, XML feeds, JSON APIs, configuration files, and more. Unlike HTML-focused template engines (Twig, Blade), Tsuku is designed specifically for data transformation.

Modern PHP Architecture ​

Built with a clean three-stage compiler pipeline:

  • Lexer: Breaks templates into tokens (and decides which whitespace around directives is layout)
  • Parser: Builds an Abstract Syntax Tree (AST), conditions included
  • Compiler: Walks the tree and generates the output

The same architecture used by programming languages like PHP itself, JavaScript, and Python.

Battle-Tested Quality ​

Tsuku 1.4.1 ships with 944 tests, 99.4% line coverage and a 98.8% mutation score. Characterization tests pin the 1.3.0 behaviour that 1.4 keeps.

Installation ​

bash
composer require qoliber/tsuku

Get Started →

Released under the MIT License.