Div 6.1.4 · Library for PHP
Turn models into any text
Div is a template engine and code generator. It merges your data with templates to write source code, configuration, documentation or HTML. Write the rules once, and let Div write the repetitive part. In PHP, developed since 2011.
Free software (GPL 3) · PHP 8.0 or newer · composer require divengine/div
{ "entity": "Product", "table": "products", "fields": [ { "name": "id", "type": "int", "sql": "INTEGER PRIMARY KEY" }, { "name": "name", "type": "string", "sql": "VARCHAR(80) NOT NULL" }, { "name": "price", "type": "float", "sql": "DECIMAL(10,2) NOT NULL" } ] }
Try it
One model, many outputs
Choose an output. Each one is a small template over the same model. The results are not mock-ups: Div 6.1.4 produced every one of them.
{ "entity": "Product", "table": "products", "fields": [ { "name": "id", "type": "int", "sql": "INTEGER PRIMARY KEY" }, { "name": "name", "type": "string", "sql": "VARCHAR(80) NOT NULL" }, { "name": "price", "type": "float", "sql": "DECIMAL(10,2) NOT NULL" } ] }
PHP class
<?php class {$entity} {[$fields] field => public {$field.type} ${$field.name};[/$fields] }
<?php class Product { public int $id; public string $name; public float $price; }
SQL table
CREATE TABLE {$table} ([$fields] field => {$field.name} {$field.sql}!$_is_last,$_is_last![/$fields] );
CREATE TABLE products ( id INTEGER PRIMARY KEY, name VARCHAR(80) NOT NULL, price DECIMAL(10,2) NOT NULL );
TypeScript
export interface {$entity} {[$fields] field => {$field.name}: {?( "{$field.type}" == "string" )?}string@else@number{/?};[/$fields] }
export interface Product { id: number; name: string; price: number; }
Markdown
# {$entity} Stored in the `{$table}` table. | Field | Type | |---|---|[$fields] field => | {$field.name} | {$field.type} |[/$fields]
# Product Stored in the `products` table. | Field | Type | |---|---| | id | int | | name | string | | price | float |
HTML form
<form class="{_entity}">[$fields] field => <label>{^field.name} <input name="{$field.name}" type="{?( "{$field.type}" == "string" )?}text@else@number{/?}"> </label>[/$fields] </form>
<form class="product"> <label>Id <input name="id" type="number"> </label> <label>Name <input name="name" type="text"> </label> <label>Price <input name="price" type="number"> </label> </form>
How Div works
It keeps going until nothing changes
Most template engines read a template once, from top to bottom. Div makes pass after pass over the whole template, and stops when a pass changes nothing.
So one pass can write template code that the next pass resolves: templates that write templates. Here, Div first turns {$list} into the name of a list, and then loops over that list.
The states on the right are a real trace of one render with Div 6.1.4, recorded with Div's log mode.
Template
[${$list}] item => - {$item}[/${$list}]
The loop's name is itself a variable: ${$list}.
In between
[$fruits] item => - {$item}[/$fruits]
Div wrote a loop over $fruits. It is ordinary Div again, so the next pass resolves it. (Left out: a step where Div only sets the loop's body aside.)
Result
- apple - pear - plum
Another pass changes nothing, so Div stops. Div also counts its passes: after 100 (the default), it stops with an error that warns of a possible endless cycle.
Dialects
Write templates in the syntax you prefer
A dialect is a small JSON file that renames Div's tags. A template names its dialect with @_DIALECT, so templates in different dialects can work together in one project. Div translates each dialect to its own syntax before it starts, so the engine behaves the same whatever the syntax.
- Make templates look like Twig, Smarty or your team's own convention.
- Keep a template valid in the format it produces, such as XML.
- Build a dialect step by step with Dialector, or in divide (Tools > Dialector).
Div's syntax
Hello {$name}! [$items] item => - {$item}[/$items]
Hello Ada! - models - templates - code
A Twig-like dialect
@_DIALECT = twig.dialect Hello {{ name }}! [$items] item => - {{ item }}[/$items]
Hello Ada! - models - templates - code
{ "DIV_TAG_REPLACEMENT_SUFFIX": " }}", "DIV_TAG_MODIFIER_SIMPLE": "{ " }
More than replacements
The tools a generator needs
Each pair shows a template and what Div 6.1.4 made of it.
Conditions
A real expression decides which part stays. Here with stock = 3, and with stock = 0 the result is Sold out.
{?( {$stock} > 0 )?}In stock: {$stock}@else@Sold out{/?}
In stock: 3
Formulas and formats
Arithmetic and number formats inside the template, using only the PHP functions Div allows.
Total: (# {$price} * {$qty} :2. #)
Total: 7.50
Includes
Templates made of other templates, such as a shared header. Templates can also inherit from each other.
{% parts/header %} <p>{$text}</p>
<header>Shop</header> <p>Fresh today.</p>
Text Div must not touch
An {ignore} block keeps code that looks like Div, such as PHP's own {$name}.
<?php class {$name}Greeter { public function greet(string $name): string { return {ignore}"Hello {$name}"{/ignore}; } }
<?php class ProductGreeter { public function greet(string $name): string { return "Hello {$name}"; } }
Three operations
Compile, transform, compose
Everything Div does is one of three operations, and you can combine them freely.
Compile
Merge a template with a model and produce the output: code, configuration, a page.
Transform
Produce a new model, by compiling to JSON, to feed the next step.
Compose
Combine the results of several runs into one deliverable.
The ecosystem
Develop Div templates in divide
divide is the IDE built for Div. It reads each template's dialect, checks templates as you type, completes variables from your real data, renders with the real engine, and replays the passes Div made with its render trace.
- Dialector builds your own dialect, section by section.
- Runner runs the steps of a generator as a Flow.
- TurboFrame is the framework divide is built on.
Install
Your first output in three steps
A complete example. You need PHP 8.0 or newer and Composer.
1. Create a project with Div
In an empty folder.
mkdir hello-div cd hello-div composer require divengine/div:6.1.4
2. Add two files
hello.tpl, the template:
Hello {$name}! [$items] item => - {$item}[/$items]
render.php, the data and the call:
<?php require 'vendor/autoload.php'; use divengine\div; echo new div('hello.tpl', [ 'name' => 'Ada', 'items' => ['models', 'templates', 'code'], ]);
3. Run it
The command:
php render.php
What Div 6.1.4 prints:
Hello Ada! - models - templates - code
Prefer an IDE? In divide, Tools > Composer packages adds Div to a project, and the help includes Div's documentation. Every example on this page, with the commands to make it again with Div 6.1.4, is in the site's repository.