summaryrefslogtreecommitdiff
path: root/src/Renderer/Twig/Extensions
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-26 12:23:47 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-29 23:46:31 +0200
commitdf6360044b5c2396b2bee0dfa9e8d744bfa424d5 (patch)
tree177b81f1feca7b1b77b4cd20d006bc1820baba12 /src/Renderer/Twig/Extensions
parentbb3d16d273bb3e4552e4869dd22cb2c2d81f5387 (diff)
Added Twig template functions
Diffstat (limited to 'src/Renderer/Twig/Extensions')
-rw-r--r--src/Renderer/Twig/Extensions/Config.php31
-rw-r--r--src/Renderer/Twig/Extensions/Globals.php23
-rw-r--r--src/Renderer/Twig/Extensions/Session.php31
-rw-r--r--src/Renderer/Twig/Extensions/Url.php43
4 files changed, 128 insertions, 0 deletions
diff --git a/src/Renderer/Twig/Extensions/Config.php b/src/Renderer/Twig/Extensions/Config.php
new file mode 100644
index 00000000..dbbe93e7
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Config.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Engelsystem\Config\Config as EngelsystemConfig;
+use Twig_Extension as TwigExtension;
+use Twig_Function as TwigFunction;
+
+class Config extends TwigExtension
+{
+ /** @var EngelsystemConfig */
+ protected $config;
+
+ /**
+ * @param EngelsystemConfig $config
+ */
+ public function __construct(EngelsystemConfig $config)
+ {
+ $this->config = $config;
+ }
+
+ /**
+ * @return TwigFunction[]
+ */
+ public function getFunctions()
+ {
+ return [
+ new TwigFunction('config', [$this->config, 'get']),
+ ];
+ }
+}
diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php
new file mode 100644
index 00000000..f9bffbc8
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Globals.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Twig_Extension as TwigExtension;
+use Twig_Extension_GlobalsInterface as GlobalsInterface;
+
+class Globals extends TwigExtension implements GlobalsInterface
+{
+ /**
+ * Returns a list of global variables to add to the existing list.
+ *
+ * @return array An array of global variables
+ */
+ public function getGlobals()
+ {
+ global $user;
+
+ return [
+ 'user' => isset($user) ? $user : [],
+ ];
+ }
+}
diff --git a/src/Renderer/Twig/Extensions/Session.php b/src/Renderer/Twig/Extensions/Session.php
new file mode 100644
index 00000000..4690f701
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Session.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
+use Twig_Extension as TwigExtension;
+use Twig_Function as TwigFunction;
+
+class Session extends TwigExtension
+{
+ /** @var SymfonySession */
+ protected $session;
+
+ /**
+ * @param SymfonySession $session
+ */
+ public function __construct(SymfonySession $session)
+ {
+ $this->session = $session;
+ }
+
+ /**
+ * @return TwigFunction[]
+ */
+ public function getFunctions()
+ {
+ return [
+ new TwigFunction('session_get', [$this->session, 'get']),
+ ];
+ }
+}
diff --git a/src/Renderer/Twig/Extensions/Url.php b/src/Renderer/Twig/Extensions/Url.php
new file mode 100644
index 00000000..62e59782
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Url.php
@@ -0,0 +1,43 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Engelsystem\Http\UrlGenerator;
+use Twig_Extension as TwigExtension;
+use Twig_Function as TwigFunction;
+
+class Url 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('url', [$this, 'getUrl']),
+ ];
+ }
+
+ /**
+ * @param string $path
+ * @param array $parameters
+ * @return UrlGenerator|string
+ */
+ public function getUrl($path, $parameters = [])
+ {
+ $path = str_replace('_', '-', $path);
+
+ return $this->urlGenerator->to($path, $parameters);
+ }
+}