summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-10 14:40:33 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-10 14:40:33 +0200
commit4bf3a68f43e4fcea34fe0a16cb3e1eecb97d332a (patch)
tree736cdc6a8df17b12d9f18312eaf09637298caabf /src
parent222c9fed7d4ca2b3b44c05907fbb7080c1efd342 (diff)
Fixed assets rendering
Diffstat (limited to 'src')
-rw-r--r--src/Renderer/Twig/Extensions/Assets.php42
-rw-r--r--src/Renderer/TwigServiceProvider.php2
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,