Add content filter to get the parsed contents of a page (lazy loading)

This commit is contained in:
Daniel Rudolf 2015-11-06 01:08:31 +01:00
parent b5d54d05c4
commit cd7cd374bb
4 changed files with 29 additions and 8 deletions

View file

@ -179,7 +179,7 @@ to use in your theme. Please note that paths (e.g. `{{ base_dir }}`) and URLs
* `{{ content }}` - The content of the current page
(after it has been processed through Markdown)
* `{{ pages }}` - A collection of all the content pages in your site
* `{{ page.id }}` - The relative path to the content file
* `{{ page.id }}` - The relative path to the content file (unique ID)
* `{{ page.url }}` - The URL to the page
* `{{ page.title }}` - The title of the page (YAML header)
* `{{ page.description }}` - The description of the page (YAML header)
@ -187,7 +187,10 @@ to use in your theme. Please note that paths (e.g. `{{ base_dir }}`) and URLs
* `{{ page.time }}` - The timestamp derived from the `Date` header
* `{{ page.date }}` - The date of the page (YAML header)
* `{{ page.date_formatted }}` - The formatted date of the page
* `{{ page.raw_content }}` - The raw, not yet parsed contents of the page
* `{{ page.raw_content }}` - The raw, not yet parsed contents of the page;
use Twigs `content` filter to get the parsed
contents of a page by passing its unique ID
(e.g. `{{ "sub/page"|content }}`)
* `{{ page.meta }}`- The meta values of the page
* `{{ prev_page }}` - The data of the previous page (relative to `current_page`)
* `{{ current_page }}` - The data of the current page

View file

@ -307,7 +307,7 @@ class Pico
// parse file content
$this->triggerEvent('onContentParsing', array(&$this->rawContent));
$this->content = $this->prepareFileContent($this->rawContent);
$this->content = $this->prepareFileContent($this->rawContent, $this->meta);
$this->triggerEvent('onContentPrepared', array(&$this->content));
$this->content = $this->parseFileContent($this->content);
@ -786,9 +786,10 @@ class Pico
* @see Pico::parseFileContent()
* @see Pico::getFileContent()
* @param string $rawContent raw contents of a page
* @param array $meta meta data to use for %meta.*% replacement
* @return string contents prepared for parsing
*/
public function prepareFileContent($rawContent)
public function prepareFileContent($rawContent, array $meta)
{
// remove meta header
$metaHeaderPattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
@ -814,9 +815,9 @@ class Pico
$content = str_replace('%theme_url%', $themeUrl, $content);
// replace %meta.*%
if (!empty($this->meta)) {
if (!empty($meta)) {
$metaKeys = $metaValues = array();
foreach ($this->meta as $metaKey => $metaValue) {
foreach ($meta as $metaKey => $metaValue) {
if (is_scalar($metaValue) || ($metaValue === null)) {
$metaKeys[] = '%meta.' . $metaKey . '%';
$metaValues[] = strval($metaValue);
@ -1073,7 +1074,24 @@ class Pico
$twigLoader = new Twig_Loader_Filesystem($this->getThemesDir() . $this->getConfig('theme'));
$this->twig = new Twig_Environment($twigLoader, $this->getConfig('twig_config'));
$this->twig->addExtension(new Twig_Extension_Debug());
// register link filter
$this->twig->addFilter(new Twig_SimpleFilter('link', array($this, 'getPageUrl')));
// register content filter
$pico = $this;
$pages = &$this->pages;
$this->twig->addFilter(new Twig_SimpleFilter('content', function ($pageId) use ($pico, &$pages) {
if (isset($pages[$pageId])) {
$pageData = &$pages[$pageId];
if (!isset($pageData['content'])) {
$pageData['content'] = $pico->prepareFileContent($pageData['raw_content'], $pageData['meta']);
$pageData['content'] = $pico->parseFileContent($pageData['content']);
}
return $pageData['content'];
}
return '';
}));
}
/**

View file

@ -33,7 +33,7 @@ class PicoParsePagesContent extends AbstractPicoPlugin
public function onSinglePageLoaded(&$pageData)
{
if (!isset($pageData['content'])) {
$pageData['content'] = $this->prepareFileContent($pageData['raw_content']);
$pageData['content'] = $this->prepareFileContent($pageData['raw_content'], $pageData['meta']);
$pageData['content'] = $this->parseFileContent($pageData['content']);
}
}

View file

@ -194,7 +194,7 @@ class DummyPlugin extends AbstractPicoPlugin
* @param string &$content prepared file contents for parsing
* @return void
*/
public function prepareFileContent(&$content)
public function onContentPrepared(&$content)
{
// your code
}