PHPackages                             jascha030/twig-service - 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. jascha030/twig-service

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

jascha030/twig-service
======================

Simple twig service interface and class I like to wrap around the Environment object during development.

1.2.0(3y ago)057[1 PRs](https://github.com/jascha030/twig-service/pulls)1MITPHPPHP ^8

Since Feb 21Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jascha030/twig-service)[ Packagist](https://packagist.org/packages/jascha030/twig-service)[ RSS](/packages/jascha030-twig-service/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (3)Versions (7)Used By (1)

Twig Service
============

[](#twig-service)

Simple set of two interfaces and one basic implementation of a service class for usage with [Twig](https://github.com/twigphp/Twig).

Getting Started
---------------

[](#getting-started)

### Requirements

[](#requirements)

- PHP `^8`
- Composer `*` (but preferred `2.2` or later)

### Installation

[](#installation)

```
composer require jascha030/twig-service
```

Usage
-----

[](#usage)

The package contents consist of one simple service class implementing, an interface, which extends one extra interface that implements the features that are independent from `twig/twig`.

### TwigServiceInterface

[](#twigserviceinterface)

The main interface is the `Jascha030\Twig\TwigServiceInterface`, which requires a class to implement three methods.

#### Methods

[](#methods)

**getEnvironment**

Get the TwigEnvironment.

`TwigServiceInterface::getEnvironment(): \Twig\Environment`

- returns `\Twig\Environment` instance.

**Inherited methods**

The methods below are inherited from `Jascha030\Twig\Templater\TemplaterInterface`.

This is done to open future possibility to replace your Twig-specific implementation with another templating method, without having to refactor your codebase, for example, by using a DIC.

> For this reason, using container bindings bound to the `TemplaterInterface` instead of the `TwigServiceInterface`. For the same reason it is recommended to let other classes depend on the `TemplaterInterface`. For dependent classes, the fact that the service uses a `Twig\Environment` instance to render the output, should not be relevant.

**renderString**

Render a template and return output as string.

`TemplaterInterface::renderString(string $template, array $context = []): string`

- parameters
    - `$template` (string): the twig template name.
    - `$context` (array): key `=>` pair values corresponding to the template variables.
- returns the rendered template as `string`

**render**

Render and output a template.

`TemplaterInterface::render(string $template, array $context = []): void`

- parameters
    - `$template` (string): the twig template name.
    - `$context` (array): key `=>` pair values corresponding to the template variables.
- returns `void` (output is expected to be echoed).

### Default Implementation

[](#default-implementation)

This package also provides class as most-basic implementation of the `TwigServiceInterface`, `Jascha030\Twig\TwigService.php`. This class implements all three methods with the addition of a constructor requiring the `Twig\Environment`.

**Below is a simple example of constructing the service with an Environment using the FilesystemLoader:**

```
