summaryrefslogtreecommitdiff
path: root/src/Renderer/Twig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Renderer/Twig')
-rw-r--r--src/Renderer/Twig/Extensions/Assets.php42
1 files changed, 42 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);
+ }
+}