Maslosoft Staple API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/staple
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @link http://maslosoft.com/staple/
*/
namespace Maslosoft\Staple\Processors\Pre;
use Maslosoft\Staple\Interfaces\PreProcessorInterface;
use Maslosoft\Staple\Interfaces\RendererAwareInterface;
/**
* ##Tag Extractor##
*
* This class extracts selected tags from view and pass it to layout.
*
* Tags can have any name, they do *not* have to be html compliant,
* as they are used only internally.
*
* Tags will be removed from view. Should not contain any attributes,
* as they are extracted in a very simple and not recommended (but fast) method.
*
* If tag contains any attribute it will not be processed.
*
* Example tags:
* ```html
* <title>My page title</title>
* ```
* This will be passed to template and can be used, for instance in document head as a title:
* ```php
* <head>
* <title><?= $this->data->title;?></title>
* </head>
* ```
*
* This can also be used with Template Applier to use different template for any page:
* ```html
* <template>product</template>
* ```
* This will lookup for `product.php` file in your layouts directory. If template
* exists it will be used by Template Applier (if it's enabled).
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class TagExtractor implements PreProcessorInterface
{
public $tags = [
'template',
'title',
'description'
];
public function decorate(RendererAwareInterface $owner, &$content, $data)
{
foreach ($this->tags as $tag)
{
$matches = [];
$pattern = $this->_getPattern($tag);
if (preg_match($pattern, $content, $matches))
{
$content = preg_replace($pattern, '', $content);
}
}
}
public function getData(RendererAwareInterface $owner, $filename, $view)
{
$data = [];
$content = '';
if (!empty($filename))
{
if (file_exists($filename))
{
$content = file_get_contents($filename);
}
else
{
return $data;
}
}
foreach ($this->tags as $tag)
{
$matches = [];
if (preg_match($this->_getPattern($tag), $content, $matches))
{
$data[$tag] = $matches[1];
}
}
return $data;
}
private function _getPattern($tag)
{
return "~<$tag>(.+?)</$tag>~i";
}
}
API documentation generated by ApiGen