Skip to content

Commit

Permalink
Use collections for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tbreuss committed Sep 13, 2015
1 parent e0fda57 commit 1f1d180
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
34 changes: 23 additions & 11 deletions plugins/shortcode/shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,35 @@ protected function addBlocksTag()
{
$this->add('blocks', function ($options) {

// Folder with "blocks"
$page = Herbie\DI::get('Page');
$path = Herbie\DI::get('Alias')->get($page->path);
$info = pathinfo($path);
$dir = $info['dirname'] . '/_' . $info['filename'];
$options = array_merge([
'path' => Herbie\DI::get('Page')->getDefaultBlocksPath(),
'sort' => '',
'shuffle' => 'false'
], (array)$options);

// collect pages
$extensions = $this->config->get('pages.extensions', []);
$path = $options['path'];
$paths = [$path => Herbie\DI::get('Alias')->get($path)];
$pageBuilder = new Herbie\Menu\Page\Builder($paths, $extensions);
$collection = $pageBuilder->buildCollection();

if (!empty($options['sort'])) {
list($field, $direction) = explode('|', $options['sort']);
$collection = $collection->sort($field, $direction);
}

if ('true' == strtolower($options['shuffle'])) {
$collection = $collection->shuffle();
}

$loader = Herbie\DI::get('Loader\PageLoader');
$twig = Herbie\DI::get('Twig');
$scanned = is_dir($dir) ? array_diff(scandir($dir), ['..', '.']) : [];

ob_start();

foreach ($scanned as $i => $filename) {
foreach ($collection as $i => $item) {

$block = new Herbie\Page();
$block->setLoader($loader);
$block->load("{$dir}/{$filename}");
$block = Herbie\Page::create($item->path);

if (!empty($block->layout) && ($block->layout == 'default.html')) {
$block->layout = false;
Expand Down
22 changes: 22 additions & 0 deletions system/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,26 @@ public function setError(\Exception $e)
];
}

/**
* @param string $alias
* @return static
*/
public static function create($alias)
{
$loader = DI::get('Loader\PageLoader');
$page = new static();
$page->setLoader($loader);
$page->load($alias);
return $page;
}

/**
* @return string
*/
public function getDefaultBlocksPath()
{
$pathinfo = pathinfo($this->path);
return $pathinfo['dirname'] . '/_' . $pathinfo['filename'];
}

}

0 comments on commit 1f1d180

Please sign in to comment.