Search by

ocubom / twig-svg-extension

ocubom

A custom suite of Twig filters for SVG manipulation

Package info

github.com/ocubom/twig-svg-extension

pkg:composer/ocubom/twig-svg-extension

Statistics

Installs: 2 891

Dependents: 1

Suggesters: 1

Stars: 4

Open Issues: 0

v2.3.3 2026-09-07 12:44 UTC

This package is auto-updated.

Last update: 2026-09-07 12:45:41 UTC


README

ocubom/twig-svg-extension

A custom suite of Twig filters for SVG manipulation

Version PHP version License Build status Coverage

Downloads Issues Stargazers Forks Contributors

Explore the docs » · Report Bug · Request Feature

Table of Contents

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 Medinahttps://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.