simsoft/twig

A Twig wrapper, built for Twig template engine.

1.0.0 2024-04-04 05:23 UTC

This package is auto-updated.

Last update: 2024-11-04 06:54:49 UTC


README

A Twig wrapper, built for Twig template engine.

Install

composer require simsoft/twig

Basic Usage

Examples setup in bootstrap or entry script file.

<?php
require "vendor/autoload.php";

use Simsoft\Twig\Twig;

$twig = new Twig([
    'path' => 'path/to/templates',
    'fileExtension' => '.twig',
    'debug' => true, // default is false
    'charset' => 'UTF-8',
    'cache' => 'path/to/cache',
    'extensions' => [new \App\MyExtension()],
    'namespaces' => [
        'name' => '/path/to/template',
    ],
]);

$twig->display('template_name', ['name' => 'John']);

Building Extension

For further tutorial, please refer to Extending Twig.

<?php

namespace App;

use use Simsoft\Twig\Extension;

class MyExtension extends Extension
{
    public function getGlobals() : array
    {
        return [
            'guest_name' => 'John Doe',
        ];
    }

    public function init(): void
    {
        // add filters
        $this->addFilter('obj_to_array', fn ($object) => (array)$object);

        // add functions
        $this->addFunction('dump', fn(...$args) => call_user_func_array('var_dump', $args));

        // add test
        $this->addTest('red', function ($value) {
            if (isset($value->color) && $value->color == 'red') {
                return true;
            }
            if (isset($value->paint) && $value->paint == 'red') {
                return true;
            }
            return false;
        });
    }
}

Building Template files.

Please refer to Twig for Template Designers.

License

The Simsoft Validator is licensed under the MIT License. See the LICENSE file for details