Add support for listing custom page templates/types

This commit is contained in:
Matias Griese
2014-10-08 12:26:45 +03:00
parent df475af2fd
commit 0902840aa4
4 changed files with 87 additions and 7 deletions

View File

@@ -34,7 +34,7 @@ form:
type:
type: select
classes: fancy
label: Page Type
label: Display Template
default: default
@data-options: '\Grav\Common\Page\Pages::types'
validate:

View File

@@ -89,12 +89,17 @@ abstract class Folder
$compare = isset($params['compare']) ? 'get' . $params['compare'] : null;
$pattern = isset($params['pattern']) ? $params['pattern'] : null;
$filters = isset($params['filters']) ? $params['filters'] : null;
$recursive = isset($params['recursive']) ? $params['recursive'] : true;
$key = isset($params['key']) ? 'get' . $params['key'] : null;
$value = isset($params['value']) ? 'get' . $params['value'] : 'getSubPathname';
$value = isset($params['value']) ? 'get' . $params['value'] : ($recursive ? 'getSubPathname' : 'getFilename');
$directory = new \RecursiveDirectoryIterator($path,
\RecursiveDirectoryIterator::SKIP_DOTS + \FilesystemIterator::UNIX_PATHS + \FilesystemIterator::CURRENT_AS_SELF);
$iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
if ($recursive) {
$directory = new \RecursiveDirectoryIterator($path,
\RecursiveDirectoryIterator::SKIP_DOTS + \FilesystemIterator::UNIX_PATHS + \FilesystemIterator::CURRENT_AS_SELF);
$iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
} else {
$iterator = new \FilesystemIterator($path);
}
$results = array();

View File

@@ -316,9 +316,19 @@ class Pages
*/
static public function types()
{
$blueprints = new Blueprints('theme://blueprints/');
static $types;
return $blueprints->types();
if (!$types) {
$types = new Types();
$types->scanBlueprints('theme://blueprints/');
$types->scanTemplates('theme://templates/');
$event = new Event();
$event->types = $types;
Grav::instance()->fireEvent('onGetPageTemplates', $event);
}
return $types->toSelect();
}
/**

View File

@@ -0,0 +1,65 @@
<?php
namespace Grav\Common\Page;
use Grav\Common\Filesystem\Folder;
use RocketTheme\Toolbox\ArrayTraits\Constructor;
use RocketTheme\Toolbox\ArrayTraits\Countable;
use RocketTheme\Toolbox\ArrayTraits\Export;
use RocketTheme\Toolbox\ArrayTraits\Iterator;
class Types
{
use Constructor, Iterator, Countable, Export;
protected $items = [];
public function register($type, $blueprint = null)
{
if ($blueprint || empty($this->items[$type])) {
$this->items[$type] = $blueprint;
}
}
public function scanBlueprints($path)
{
$options = [
'compare' => 'Filename',
'pattern' => '|\.yaml$|',
'filters' => [
'key' => '|\.yaml$|'
],
'key' => 'Filename',
'value' => 'PathName',
'recursive' => false
];
$this->items = Folder::all($path, $options) + $this->items;
}
public function scanTemplates($path)
{
$options = [
'compare' => 'Filename',
'pattern' => '|\.html\.twig$|',
'filters' => [
'value' => '|\.html\.twig$|'
],
'value' => 'Filename',
'recursive' => false
];
foreach (Folder::all($path, $options) as $type) {
$this->register($type);
}
}
public function toSelect()
{
$list = [];
foreach ($this->items as $name => $file) {
$list[$name] = ucfirst(strtr($name, '_', ' '));
}
ksort($list);
return $list;
}
}