PHP code example of ocubom / twig-svg-extension

1. Go to this page and download the library: Download ocubom/twig-svg-extension library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ocubom / twig-svg-extension example snippets


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;
        }
    }
});