diff options
Diffstat (limited to 'src')
-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 0f453989..c70fb303 100644 --- a/src/Renderer/TwigServiceProvider.php +++ b/src/Renderer/TwigServiceProvider.php @@ -3,6 +3,7 @@ namespace Engelsystem\Renderer; use Engelsystem\Container\ServiceProvider; +use Engelsystem\Renderer\Twig\Extensions\Assets; use Engelsystem\Renderer\Twig\Extensions\Config; use Engelsystem\Renderer\Twig\Extensions\Globals; use Engelsystem\Renderer\Twig\Extensions\Session; @@ -15,6 +16,7 @@ class TwigServiceProvider extends ServiceProvider { /** @var array */ protected $extensions = [ + 'assets' => Assets::class, 'config' => Config::class, 'globals' => Globals::class, 'session' => Session::class, |