KTemplate is a simple text template engine for PHP and KPHP.
KTemplate uses a syntax similar to the Twig, Django and Jinja template languages.
You can try it online!
Features:
- Cross-language support: works for both PHP and KPHP
- Security: no eval or dynamic PHP code generation/loading is used
- Compile-time checks: many errors are caught during template compilation
- Zero-copy data binding: efficient and flexible data provider model
- Performance: templates are compiled to optimized bytecode
$ composer require quasilyte/ktemplate
<?php
require_once __DIR__ . '/vendor/autoload.php';
use KTemplate\Context;
use KTemplate\Engine;
use KTemplate\ArrayLoader;
use KTemplate\ArrayDataProvider;
$loader = new ArrayLoader([
'main' => '{{ title }}',
]);
$engine = new Engine(new Context(), $loader);
$data = new ArrayDataProvider(['title' => 'Example']);
$result = $engine->render('main', $data);
var_dump($result); // => "Example"
Run with PHP:
$ php -f example.php
string(7) "Example"
Run with KPHP:
# 1. Compile
$ kphp --composer-root $(pwd) --mode cli example.php
# 2. Execute
$ ./kphp_out/cli
string(7) "Example"
- Template language overview
- KTemplate idioms
- Differences from Twig
- KTemplate PHP API
- KTemplate architecture
None of the template engines for PHP can be used with KPHP.
KTemplate is a solution that works in both languages.