Пример #1
0
 protected function addBlocksTag()
 {
     $this->add('blocks', function ($options) {
         $options = array_merge(['path' => DI::get('Page')->getDefaultBlocksPath(), 'sort' => '', 'shuffle' => 'false'], (array) $options);
         // collect pages
         $extensions = $this->config->get('pages.extensions', []);
         $path = $options['path'];
         $paths = [$path => 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();
         }
         $twig = DI::get('Twig');
         // store page
         $page = DI::get('Page');
         $return = '';
         foreach ($collection as $i => $item) {
             $block = Herbie\Page::create($item->path);
             DI::set('Page', $block);
             // self-contained blocks aka widgets
             if (!empty($block->layout) && file_exists($paths[$path] . '/.layouts/' . $block->layout)) {
                 $block->layout = $path . '/.layouts/' . $block->layout;
             }
             if (empty($block->layout)) {
                 $return .= $twig->renderPageSegment(0, $block);
             } else {
                 $twig->getEnvironment()->getExtension('herbie')->setPage($block);
                 $return .= $twig->render($block->layout, ['block' => $block]);
             }
             $return .= "\n";
         }
         // restore page
         $twig->getEnvironment()->getExtension('herbie')->setPage($page);
         DI::set('Page', $page);
         return trim($return);
     });
 }
Пример #2
0
 protected function addBlocksTag()
 {
     $this->add('blocks', function ($options) {
         $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();
         }
         $twig = Herbie\DI::get('Twig');
         ob_start();
         foreach ($collection as $i => $item) {
             $block = Herbie\Page::create($item->path);
             if (!empty($block->layout) && $block->layout == 'default.html') {
                 $block->layout = false;
             }
             if (empty($block->layout)) {
                 echo $twig->renderPageSegment(0, $block);
             } else {
                 $twig->getEnvironment()->getExtension('herbie')->setPage($block);
                 echo $twig->render($block->layout);
             }
             ob_flush();
         }
         return ob_get_clean();
     });
 }