diff --git a/flextype/Snippets.php b/flextype/Snippets.php index 706091e7..d88b9409 100644 --- a/flextype/Snippets.php +++ b/flextype/Snippets.php @@ -14,10 +14,43 @@ namespace Flextype; use Flextype\Component\Filesystem\Filesystem; use Flextype\Component\Event\Event; -use Flextype\Component\I18n\I18n; -use Flextype\Component\Registry\Registry; + +// Event: onShortcodesInitialized +Event::addListener('onShortcodesInitialized', function () { + + // Shortcode: [snippet name=snippet-name] + Content::shortcode()->addHandler('snippet', function(ShortcodeInterface $s) { + return Snippet::get($s->getParameter('name')); + }); +}); class Snippets { + /** + * Get snippet + * + * Snippet::get('snippet-name'); + * + * @access public + * @param string $snippet_name Snippet name + * @return void + */ + public static function get($snippet_name) + { + $snippet_path = PATH['snippets'] . '/' . $snippet_name . '.php'; + if (Filesystem::fileExists($snippet_path)) { + + // Turn on output buffering + ob_start(); + + // Include view file + include $snippet_path; + + // Output... + return ob_get_clean(); + } else { + throw new \RuntimeException("Snippet {$snippet_path} does not exist."); + } + } }