PHPackages                             simsoft/twig - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. simsoft/twig

ActiveLibrary[Templating &amp; Views](/categories/templating)

simsoft/twig
============

A lightweight Twig wrapper that simplifies template engine setup with configuration-driven initialization, namespace support, and easy extension authoring.

2.0.0(1mo ago)084MITPHPPHP ^8.2CI passing

Since Apr 4Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/sim-soft/twig)[ Packagist](https://packagist.org/packages/simsoft/twig)[ Docs](https://github.com/simsoft/twig)[ RSS](/packages/simsoft-twig/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (5)Versions (7)Used By (0)

Simsoft Twig
============

[](#simsoft-twig)

A lightweight PHP wrapper for the [Twig 3.x](https://twig.symfony.com/) template engine. Simplifies setup with configuration-driven initialization, namespace support, and easy extension authoring.

Features
--------

[](#features)

- Configuration-based initialization (paths, caching, debug, charset, timezone)
- Built-in HTML minification for production output
- Template namespaces for organized directory structures
- Simplified extension base class with helper methods for filters, functions, and tests
- Fluent API for runtime customization
- Auto-escaping enabled by default (XSS protection)

Requirements
------------

[](#requirements)

- PHP 8.2+
- Composer

Installation
------------

[](#installation)

```
composer require simsoft/twig
```

Quick Start
-----------

[](#quick-start)

```
use Simsoft\Twig\Twig;

$twig = new Twig([
    'path' => __DIR__ . '/templates',
    'cache' => __DIR__ . '/cache',
]);

// Render to string
$html = $twig->render('hello', ['name' => 'World']);

// Output directly
$twig->display('hello', ['name' => 'World']);
```

Configuration Options
---------------------

[](#configuration-options)

OptionTypeDefaultDescription`path`string|string\[\]`/`Path(s) to templates directory`fileExtension`string`.twig`Template file extension`debug`bool`false`Enable debug mode`charset`string`UTF-8`Template charset`cache`string—Compiled template cache directory`timezone`string—Timezone for date formatting`extensions`array`[]`Array of `ExtensionInterface` instances`namespaces`array`[]`Map of namespace name → template path`minify`bool`false`Minify HTML output (removes comments, collapses whitespace)Unrecognized config keys will throw an `InvalidArgumentException` to catch typos early.

Typed Configuration (Alternative)
---------------------------------

[](#typed-configuration-alternative)

For IDE autocompletion, use the `TwigConfig` object instead of an array:

```
use Simsoft\Twig\Twig;
use Simsoft\Twig\TwigConfig;

$twig = new Twig(new TwigConfig(
    path: __DIR__ . '/templates',
    cache: __DIR__ . '/cache',
    debug: true,
    timezone: 'Asia/Kuala_Lumpur',
    minify: true,
    extensions: [new \App\MyExtension()],
    namespaces: [
        'layouts' => __DIR__ . '/templates/layouts',
    ],
));
```

Full Configuration Example
--------------------------

[](#full-configuration-example)

```
use Simsoft\Twig\Twig;

$twig = new Twig([
    'path' => __DIR__ . '/templates',
    'fileExtension' => '.twig',
    'debug' => true,
    'charset' => 'UTF-8',
    'cache' => __DIR__ . '/cache',
    'timezone' => 'Asia/Kuala_Lumpur',
    'minify' => true,
    'extensions' => [new \App\MyExtension()],
    'namespaces' => [
        'layouts' => __DIR__ . '/templates/layouts',
        'components' => __DIR__ . '/templates/components',
        'macros' => __DIR__ . '/templates/macros',
    ],
]);
```

Template Namespaces
-------------------

[](#template-namespaces)

Namespaces let you reference templates from different directories:

```
// Renders @layouts/base.twig
$twig->render('@layouts/base', ['title' => 'Home']);
```

HTML Minification
-----------------

[](#html-minification)

Enable `minify` to automatically strip HTML comments, collapse whitespace between tags, and reduce output size across all rendering methods (`render()`, `display()`, `renderBlock()`, `renderIf()`):

```
$twig = new Twig([
    'path' => __DIR__ . '/templates',
    'minify' => true, // All output is minified
]);

// This output will be minified automatically
$html = $twig->render('page', ['title' => 'Home']);
```

The static helper `Twig::minify()` is also available for one-off use on any HTML string:

```
$minified = Twig::minify($rawHtml);
```

Minification preserves IE conditional comments (`\
