Dialector · Web app for Div

Templates in the syntax you choose

A dialect is the set of tags a Div template is written with. Dialector shows every tag of Div in a sample template, lets you change each one, checks that no required tag is empty and that no tag repeats another, and gives the dialect in two forms: a file that a template names, or PHP code that changes the syntax of a whole program. divide has it built in (Tools > Dialector).

Free software (GPL 3) · runs in the browser · no release yet

  1. Change the tags{$name} becomes <%= name %>
  2. Validatenone empty, none repeated
  3. Savea file, or PHP code

Examples

What sets it apart

Each example ran with Div 6.1.4. The output is what it printed.

A dialect file, named by a template

Save gives the changed tags as a file. A template names it on its first line, so it can live next to templates in other dialects.

dialects/erb.dialect
{
  'DIV_TAG_REPLACEMENT_PREFIX': '<%',
  'DIV_TAG_REPLACEMENT_SUFFIX': ' %>',
  'DIV_TAG_MODIFIER_SIMPLE': '= ',
  'DIV_TAG_LOOP_BEGIN_PREFIX': '<% for ',
  'DIV_TAG_LOOP_BEGIN_SUFFIX': ' %>',
  'DIV_TAG_LOOP_END_PREFIX': '<% end ',
  'DIV_TAG_LOOP_END_SUFFIX': ' %>'
}
templates/order.erb
@_DIALECT = dialects/erb.dialect
Order for <%= customer %>:
<% for items %>  - <%= name %> x <%= units %>
<% end items %>
examples/saved.php
<?php
// A dialect saved from Dialector (Save) is a file. A template names it in its
// first line, @_DIALECT, so templates in different dialects live side by side.
require __DIR__ . '/../vendor/autoload.php';

use divengine\div;

$order = ['customer' => 'Ada', 'items' => [
    ['name' => 'Apple', 'units' => 3],
    ['name' => 'Pear', 'units' => 2],
]];

echo "templates/order.erb, in the dialect of dialects/erb.dialect:\n";
echo new div('templates/order.erb', $order);
echo "templates/order.tpl, in Div's own syntax:\n";
echo new div('templates/order.tpl', $order);
Output
templates/order.erb, in the dialect of dialects/erb.dialect:
Order for Ada:
  - Apple x 3
  - Pear x 2

templates/order.tpl, in Div's own syntax:
Order for Ada:
  - Apple x 3
  - Pear x 2

PHP code, for a whole program

Generate PHP Code gives define() lines. Loaded before Div, they set the syntax of every template.

dialects/blade.php
<?php

/* Div PHP Template Engine Dialect - Generated by Div Dialect Creator */

/* IMPORTANT!: Include this file before you include div.php */

define('DIV_TAG_REPLACEMENT_PREFIX', '{{');
define('DIV_TAG_REPLACEMENT_SUFFIX', ' }}');
define('DIV_TAG_MODIFIER_SIMPLE', ' ');
define('DIV_TAG_LOOP_BEGIN_PREFIX', '@foreach(');
define('DIV_TAG_LOOP_BEGIN_SUFFIX', ')');
define('DIV_TAG_LOOP_END_PREFIX', '@endforeach(');
define('DIV_TAG_LOOP_END_SUFFIX', ')');

// End of file
templates/order.blade
Order for {{ customer }}:
@foreach(items)  - {{ name }} x {{ units }}
@endforeach(items)
examples/generated.php
<?php
// Dialector's "Generate PHP Code" gives define() lines. Loaded before Div,
// they change the syntax of every template of the program.
require __DIR__ . '/../dialects/blade.php';
require __DIR__ . '/../vendor/autoload.php';

use divengine\div;

echo new div('templates/order.blade', ['customer' => 'Grace', 'items' => [
    ['name' => 'Lemon', 'units' => 6],
    ['name' => 'Fig', 'units' => 4],
]]);
Output
Order for Grace:
  - Lemon x 6
  - Fig x 4

Use it

Make a dialect

1. Build a dialect

In the browser, at dialector.divengine.org, or in divide: Tools > Dialector.

2. Render with Div

The dialects above were rendered with Div 6.1.4.

composer require divengine/div:6.1.4

Dialector has no release yet; the dialects here are in the formats of its Save and Generate PHP Code buttons. The examples, with the commands to run them again with Div 6.1.4, are in the site's repository. More on dialects on Div's page.