diff options
Diffstat (limited to 'src/Renderer')
-rw-r--r-- | src/Renderer/Twig/Extensions/Assets.php | 42 | ||||
-rw-r--r-- | src/Renderer/TwigServiceProvider.php | 2 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/Renderer/Twig/Extensions/Assets.php b/src/Renderer/Twig/Extensions/Assets.php new file mode 100644 index 00000000..2cdfb0fd --- /dev/null +++ b/src/Renderer/Twig/Extensions/Assets.php @@ -0,0 +1,42 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Engelsystem\Http\UrlGenerator; +use Twig_Extension as TwigExtension; +use Twig_Function as TwigFunction; + +class Assets extends TwigExtension +{ + /** @var UrlGenerator */ + protected $urlGenerator; + + /** + * @param UrlGenerator $urlGenerator + */ + public function __construct(UrlGenerator $urlGenerator) + { + $this->urlGenerator = $urlGenerator; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('asset', [$this, 'getAsset']), + ]; + } + + /** + * @param string $path + * @return UrlGenerator|string + */ + public function getAsset($path) + { + $path = ltrim($path, '/'); + + return $this->urlGenerator->to('/' . $path); + } +} diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php index 59ed75e0..49a0eb90 100644 --- a/src/Renderer/TwigServiceProvider.php +++ b/src/Renderer/TwigServiceProvider.php @@ -5,6 +5,7 @@ namespace Engelsystem\Renderer; use Engelsystem\Config\Config as EngelsystemConfig; use Engelsystem\Container\ServiceProvider; use Engelsystem\Renderer\Twig\Extensions\Authentication; +use Engelsystem\Renderer\Twig\Extensions\Assets; use Engelsystem\Renderer\Twig\Extensions\Config; use Engelsystem\Renderer\Twig\Extensions\Globals; use Engelsystem\Renderer\Twig\Extensions\Legacy; @@ -19,6 +20,7 @@ class TwigServiceProvider extends ServiceProvider { /** @var array */ protected $extensions = [ + 'assets' => Assets::class, 'authentication' => Authentication::class, 'config' => Config::class, 'globals' => Globals::class, |