1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-29 07:50:29 +02:00

Slim Framework integration #118 #117

- Snippets: refactoring
This commit is contained in:
Awilum
2019-05-29 00:27:34 +03:00
parent 9015a10ea7
commit b8f04df7af
2 changed files with 52 additions and 3 deletions

View File

@@ -45,7 +45,56 @@ class Snippets
$vars['fetch'] = $id;
return $this->_fetch_snippet($vars);
return $this->_display_snippet($vars);
}
/**
* Fetch snippet
*
* @access public
* @param string $id Snippet id
* @return array|false The entry contents or false on failure.
*/
public function fetch(string $id)
{
$snippet_file = Fieldsets::_file_location($id);
if (Filesystem::has($snippet_file)) {
if ($snippet_body = Filesystem::read($snippet_file)) {
return $snippet_body;
} else {
return false;
}
} else {
return false;
}
}
/**
* Fetch Snippets
*
* @access public
* @return array
*/
public function fetchList() : array
{
$snippets = [];
// Get snippets files
$_snippets = Filesystem::listContents($this->_dir_location());
// If there is any snippets file then go...
if (count($_snippets) > 0) {
foreach ($_snippets as $snippet) {
if ($snippet['type'] == 'file' && $snippet['extension'] == 'json') {
$snippet_content = JsonParser::decode(Filesystem::read($snippet['path']));
$snippet[$snippet['basename']] = $snippet_content['title'];
}
}
}
// return snippets
return $snippets;
}
/**

View File

@@ -15,7 +15,7 @@ namespace Flextype;
use Thunder\Shortcode\ShortcodeFacade;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
// Shortcode: [snippets fetch=snippet-name]
// Shortcode: [snippets id=snippet-name]
$flextype['shortcodes']->addHandler('snippets', function(ShortcodeInterface $s) use ($flextype) {
return $flextype['shortcodes']->fetch($s->getParameter('fetch'));
return $flextype['shortcodes']->display($s->getParameter('id'));
});