From 2a3ee7c1ec4b862097275f350f2ed182cdbf8c47 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 11 Jun 2019 21:50:01 +0300 Subject: [PATCH] Flextype Core: Snippets Twig Extension added #117 --- flextype/dependencies.php | 3 ++ flextype/twig/SnippetsTwigExtension.php | 46 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 flextype/twig/SnippetsTwigExtension.php diff --git a/flextype/dependencies.php b/flextype/dependencies.php index 405495e1..478b167b 100644 --- a/flextype/dependencies.php +++ b/flextype/dependencies.php @@ -209,6 +209,9 @@ $flextype['view'] = function ($container) { // Add Global Shortcodes Twig Extension $view->addExtension(new ShortcodesTwigExtension($container)); + // Add Global Snippets Twig Extension + $view->addExtension(new SnippetsTwigExtension($container)); + // Return view return $view; }; diff --git a/flextype/twig/SnippetsTwigExtension.php b/flextype/twig/SnippetsTwigExtension.php new file mode 100644 index 00000000..88dc21f5 --- /dev/null +++ b/flextype/twig/SnippetsTwigExtension.php @@ -0,0 +1,46 @@ + + * @link http://romanenko.digital + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flextype; + +class SnippetsTwigExtension extends \Twig_Extension +{ + /** + * Flextype Dependency Container + */ + private $flextype; + + /** + * Constructor + */ + public function __construct($flextype) + { + $this->flextype = $flextype; + } + + /** + * Callback for twig. + * + * @return array + */ + public function getFunctions() + { + return [ + new \Twig_SimpleFunction('snippet', [$this, 'snippet']) + ]; + } + + public function snippet(string $id) + { + return $this->flextype['snippets']->display($id); + } +}