mirror of
https://github.com/flextype/flextype.git
synced 2025-08-18 02:41:27 +02:00
Flextype Core: Snippets API #154
- added ability to access $flextype and $app inside snippets. - display() method removed. - exec() method added. - _display_snippet() method removed. - _exec_snippet() method added. - from now we will set prefix bind_ for all variables. - [snippets] shortcode removed. - [snppets_fetch] shortcode added. - SnippetsTwigExtension: snippets_exec() added. - SnippetsTwigExtension: snippet removed.
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@@ -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'));
|
||||
});
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user