diff --git a/flextype/core/Snippets.php b/flextype/core/Snippets.php index ad53c7fe..de98b067 100644 --- a/flextype/core/Snippets.php +++ b/flextype/core/Snippets.php @@ -21,27 +21,33 @@ class Snippets */ private $flextype; + /** + * Flextype Application + */ + private $app; + /** * Constructor * * @access public */ - public function __construct($flextype) + public function __construct($flextype, $app) { $this->flextype = $flextype; + $this->app = $app; } /** - * Get snippet + * Exec snippet * * @access public * @param string $id Snippet id * @return string|bool Returns the contents of the output buffer and end output buffering. * If output buffering isn't active then FALSE is returned. */ - public function display(string $id) + public function exec(string $id) { - return $this->_display_snippet(['fetch' => $id]); + return $this->_exec_snippet(['id' => $id]); } /** @@ -93,7 +99,7 @@ class Snippets } /** - * Rename snippet. + * Rename snippet * * @access public * @param string $id Snippet id @@ -182,23 +188,23 @@ class Snippets } /** - * Helper private method _display_snippet + * Helper private method _exec_snippet * * @access private * @param array $vars Vars * @return string|bool Returns the contents of the output buffer and end output buffering. * If output buffering isn't active then FALSE is returned. */ - private function _display_snippet(array $vars) + private function _exec_snippet(array $vars) { - // Extracst attributes - extract($vars); + // Extracts vars and set prefix bind_ for all of them + extract($vars, EXTR_PREFIX_ALL, 'bind'); - // Get snippet name - $name = (isset($fetch)) ? (string) $fetch : ''; + // Get snippet id + $snippet_id = (string) $bind_id ?? ''; - // Define snippet path - $snippet_file = $this->_file_location($name); + // Define snippet file path + $snippet_file = $this->_file_location($snippet_id); // Process snippet if (Filesystem::has($snippet_file)) { @@ -206,13 +212,19 @@ class Snippets // Turn on output buffering ob_start(); - // Include view file + // Re-init $flextype for snippets + $flextype = $this->flextype; + + // Re-init $app for snippets + $app = $this->app; + + // Include snippet file include $snippet_file; // Output... return ob_get_clean(); } else { - throw new \RuntimeException("Snippet {$name} does not exist."); + throw new \RuntimeException("Snippet {$snippet_id} does not exist."); } } diff --git a/flextype/dependencies.php b/flextype/dependencies.php index 478b167b..492c7183 100644 --- a/flextype/dependencies.php +++ b/flextype/dependencies.php @@ -140,8 +140,8 @@ $flextype['fieldsets'] = function ($container) use ($flextype) { /** * Add snippets service to Flextype container */ -$flextype['snippets'] = function ($container) use ($flextype) { - return new Snippets($flextype); +$flextype['snippets'] = function ($container) use ($flextype, $app) { + return new Snippets($flextype, $app); }; /** diff --git a/flextype/shortcodes/SnippetsShortcode.php b/flextype/shortcodes/SnippetsShortcode.php index f4754fa0..0990f878 100644 --- a/flextype/shortcodes/SnippetsShortcode.php +++ b/flextype/shortcodes/SnippetsShortcode.php @@ -15,7 +15,7 @@ namespace Flextype; use Thunder\Shortcode\ShortcodeFacade; use Thunder\Shortcode\Shortcode\ShortcodeInterface; -// Shortcode: [snippets fetch=snippet-name] +// Shortcode: [snippets_fetch id="snippet-name"] $flextype['shortcodes']->addHandler('snippets', function (ShortcodeInterface $s) use ($flextype) { - return $flextype['snippets']->display($s->getParameter('fetch')); + return $flextype['snippets']->exec($s->getParameter('id')); }); diff --git a/flextype/twig/SnippetsTwigExtension.php b/flextype/twig/SnippetsTwigExtension.php index 88dc21f5..4ef42fa1 100644 --- a/flextype/twig/SnippetsTwigExtension.php +++ b/flextype/twig/SnippetsTwigExtension.php @@ -35,12 +35,12 @@ class SnippetsTwigExtension extends \Twig_Extension public function getFunctions() { return [ - new \Twig_SimpleFunction('snippet', [$this, 'snippet']) + new \Twig_SimpleFunction('snippets_exec', [$this, 'exec']) ]; } - public function snippet(string $id) + public function exec(string $id) { - return $this->flextype['snippets']->display($id); + return $this->flextype['snippets']->exec($id); } }