Download the PHP package jascha030/twig-service without Composer
On this page you can find all versions of the php package jascha030/twig-service. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jascha030/twig-service
More information about jascha030/twig-service
Files in jascha030/twig-service
Package twig-service
Short Description Simple twig service interface and class I like to wrap around the Environment object during development.
License MIT
Informations about the package twig-service
Twig Service
Simple set of two interfaces and one basic implementation of a service class for usage with Twig.
Getting Started
Requirements
- PHP
^8
- Composer
*
(but preferred2.2
or later)
Installation
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
The main interface is the Jascha030\Twig\TwigServiceInterface
, which requires a class to implement three 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 theTwigServiceInterface
. For the same reason it is recommended to let other classes depend on theTemplaterInterface
. For dependent classes, the fact that the service uses aTwig\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
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:
Development
Clone this repo, and run composer install
inside the repo.
Code-style
A code-style is provided in the form of a php-cs-fixer
configuration in .php-cs-fixer.dist.php
. For easy execution,
use the provided Composer script command.
If you have php-cs-fixer installed globally, pass it to the --config
argument of the fix
command.
Unit-testing
A configuration for phpunit
is provided in phpunit.xml
.
For easy execution, use the provided Composer script command.
If you have phpunit installed globally, and want to use that, pass the config in the --config
argument.
Motivation
My personal preference is to wrap the \Twig\Environment
class in a service class, which I was repeatedly writing
again, while it's a very simple class.
So (finally) I added them to a simple package, complete with unit-tests.
License
This composer package is an open-sourced software licensed under the MIT License
Note: to find the right license for your project use GitHub's https://choosealicense.com/, or read up on any other information, regarding Licensing your project in their docs' page on licensing.