quasilyte/ktemplate

v0.8.1 2024-04-02 18:45 UTC

This package is not auto-updated.

Last update: 2024-11-13 19:16:29 UTC


README

Overview

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

Quick Start

$ 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"

Documentation

Rationale

None of the template engines for PHP can be used with KPHP.

KTemplate is a solution that works in both languages.