mirror of
https://github.com/flextype/flextype.git
synced 2025-08-16 18:14:04 +02:00
Snippets: Implementation
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user