ocubom / twig-svg-extension
A custom suite of Twig filters for SVG manipulation
Requires
- php: >=7.4
- ext-dom: *
- ext-libxml: *
- bentools/iterable-functions: ^2.0
- iconify/json-tools: ^1.0
- masterminds/html5: ^2.0
- ocubom/base-convert: ^2.0|^3.0
- symfony/deprecation-contracts: ^2.5|^3.2
- symfony/options-resolver: ^5.4|^6.0|^7.0|^8.0
- twig/twig: ^2.7|^3.0
Requires (Dev)
- enshrined/svg-sanitize: *
- friendsofphp/php-cs-fixer: *
- iconify/json: *
- phpstan/phpstan: ^2.2
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- symfony/phpunit-bridge: ^4.4.9|^5.0.9|^6.0|^7.0|^8.0
Suggests
- enshrined/svg-sanitize: Allows using a SVG sanitizer
Provides
None
Conflicts
None
Replaces
None
README
A custom suite of Twig filters for SVG manipulation
Explore the docs » · Report Bug · Request Feature
About ocubom/twig-svg-extension
ocubom/twig-svg-extension is a custom suite of Twig extensions for SVG manipulation.
This suite started as an internal class to inline SVG files into HTML documents. Over time, as several projects embedded and modified this class independently, development became fragmented and difficult to maintain. This repository unifies those efforts into a single, open-source package.
Getting Started
Prerequisites
- PHP >= 7.4 (tested up to 8.5)
- Composer >= 2.0
Installation
Install the package via composer:
composer require ocubom/twig-svg-extension
Usage
Register the extension and runtime loaders in your Twig environment:
use Twig\Environment; use Twig\RuntimeLoader\FactoryRuntimeLoader; use Twig\RuntimeLoader\RuntimeLoaderInterface; use Ocubom\Twig\Extension\SvgExtension; use Ocubom\Twig\Extension\SvgRuntime; use Ocubom\Twig\Extension\Svg\Provider\FileSystem\FileSystemLoader; use Ocubom\Twig\Extension\Svg\Provider\FontAwesome\FontAwesomeLoader; use Ocubom\Twig\Extension\Svg\Provider\FontAwesome\FontAwesomeRuntime; use Ocubom\Twig\Extension\Svg\Provider\Iconify\IconifyLoader; use Ocubom\Twig\Extension\Svg\Provider\Iconify\IconifyRuntime; $twig = new Environment($loader); $twig->addExtension(new SvgExtension()); $twig->addRuntimeLoader(new FactoryRuntimeLoader([ SvgRuntime::class => function () use ($paths) { return new SvgRuntime(new FileSystemLoader($paths)); }, FontAwesomeRuntime::class => function () use ($paths) { return new FontAwesomeRuntime(new FontAwesomeLoader($paths)); }, IconifyRuntime::class => function () use ($paths) { return new IconifyRuntime(new IconifyLoader($paths)); }, ])); // You can also dynamically create a RuntimeLoader // You can also dynamically create a RuntimeLoader $twig->addRuntimeLoader(new class($paths) implements RuntimeLoaderInterface { /** @var mixed */ private $paths; public function __construct($paths) { $this->paths = $paths; } public function load($class) { switch ($class) { case SvgRuntime::class: return new SvgRuntime(new FileSystemLoader($this->paths)); case FontAwesomeRuntime::class: return new FontAwesomeRuntime(new FontAwesomeLoader($this->paths)); case IconifyRuntime::class: return new IconifyRuntime(new IconifyLoader($this->paths)); default: return null; } } }); $twig->addRuntimeLoader(new class() implements RuntimeLoaderInterface { public function load($class) { switch ($class) { case SvgRuntime::class: return new SvgRuntime(new FileSystemLoader($this->paths)); case FontAwesomeRuntime::class: return new FontAwesomeRuntime(new FontAwesomeLoader($this->paths)); case IconifyRuntime::class: return new IconifyRuntime(new IconifyLoader($this->paths)); default: return null; } } });
Roadmap
See the open issues for a list of proposed features (and known issues).
Support
Reach out to the maintainer at GitHub Issues.
Authorship
Oscar Cubo Medina — https://ocubom.github.io
For a full list of all authors and contributors, see the contributors page.
License
Distributed under the MIT License.
See LICENSE for more information.