diff --git a/README.md b/README.md index 9085736..c4c9fc0 100644 --- a/README.md +++ b/README.md @@ -118,16 +118,16 @@ slices. The blocks do work over multiple lines. Here is an example of a block and slice you could place in your template. ``` -{BoldBlock name='World'}Hello{/BoldBlock} it is {TimeSlice} +{Bold name='World'}Hello{/Bold} it is {Time} ``` -And here is the class you could write. Name it BoldBlock.php and place it in your +And here is the class you could write. Name it Bold.php and place it in your plugin folder. ```php namespace SurfStack\Templating\Plugin; -class BoldBlock extends Block +class Bold extends Block { function render($strContent, $arrData) { @@ -136,13 +136,13 @@ class BoldBlock extends Block } ``` -Here is the code for the slice. Name it TimeSlice.php and place it in your plugin +Here is the code for the slice. Name it Time.php and place it in your plugin folder. ```php namespace SurfStack\Templating\Plugin; -class TimeSlice extends Slice +class Time extends Slice { function render($arrData) { diff --git a/src/SurfStack/Templating/Plugin/Block.php b/src/SurfStack/Templating/Plugin/Block.php index db34fb4..49dc034 100644 --- a/src/SurfStack/Templating/Plugin/Block.php +++ b/src/SurfStack/Templating/Plugin/Block.php @@ -19,6 +19,12 @@ */ abstract class Block { + /** + * Array of settings from Template Engine + * @var array + */ + public $internal = array(); + /** * Called by the template, expects a return value * @param string $strContent diff --git a/src/SurfStack/Templating/Plugin/Slice.php b/src/SurfStack/Templating/Plugin/Slice.php index 96c27bb..71eb57c 100644 --- a/src/SurfStack/Templating/Plugin/Slice.php +++ b/src/SurfStack/Templating/Plugin/Slice.php @@ -19,6 +19,12 @@ */ abstract class Slice { + /** + * Array of settings from Template Engine + * @var array + */ + public $internal = array(); + /** * Called by the template, expects a return value * @param array $arrData diff --git a/src/SurfStack/Templating/Template_Engine.php b/src/SurfStack/Templating/Template_Engine.php index b8f56ab..64f724f 100644 --- a/src/SurfStack/Templating/Template_Engine.php +++ b/src/SurfStack/Templating/Template_Engine.php @@ -44,8 +44,6 @@ class Template_Engine */ function __construct($template) { - $this->setTemplate($template); - // Set the default settings $this->internal = array( 'StripTags' => false, @@ -55,6 +53,8 @@ function __construct($template) 'CacheLifetime' => 3600, 'AlwaysCheckOriginal' => false, ); + + $this->setTemplate($template); } /** @@ -72,6 +72,9 @@ function setTemplate($template) // Store the template full path $this->template = stream_resolve_include_path($template); + // Store the template directory + $this->setInternal('TemplateDir', dirname($this->template)); + if (strstr(get_include_path(), dirname($this->template)) === false) { // Set the include path to include the template directory @@ -590,20 +593,21 @@ protected function loadPlugins() { foreach(glob($this->getInternal('PluginDir').'/*.php') as $file) { - $f = basename($file); + require_once $file; - $name = str_replace('.php', '', $f); - - if (strpos($f, 'Block.php') !== false) - { - $return[$name] = '/\{\s*('.$name.')\s*(.*?)\}(.[^\}\{]*?)\{\/\s*'.$name.'\s*\}/i'; - } - else if (strpos($f, 'Slice.php') !== false) + $name = str_replace('.php', '', basename($file)); + + $parent = basename(get_parent_class('\SurfStack\Templating\Plugin\\'.$name)); + + switch ($parent) { - $return[$name] = '/\{\s*('.$name.')\s*(.*?)\}/i'; + case 'Block': + $return[$name] = '/\{\s*('.$name.')\s*(.*?)\}(.[^\}\{]*?)\{\/\s*'.$name.'\s*\}/i'; + break; + case 'Slice': + $return[$name] = '/\{\s*('.$name.')\s*(.*?)\}/i'; + break; } - - require_once $file; } } } @@ -649,15 +653,14 @@ protected function callPluginDynamic(array $matches) $arr = array(); - // Break into words - if(preg_match_all('/(\w+|"[\w\s]*"|\'[\w\s]*\')+/', $pluginData, $m)) + if(preg_match_all('/(\w+=\'[^\']*\'|\w+=\"[^"]*"|\w+=[^\s]*)+/', $pluginData, $m)) { + foreach($m[0] as $key => $k) { - if ($key % 2 != 0) - { - $arr[trim($m[0][$key-1], '"..\'')] = trim($k, '"..\''); - } + $arrSplit = explode('=', $k); + + $arr[$arrSplit[0]] = trim(join('', array_slice($arrSplit, 1)), '\'\"'); } } @@ -672,6 +675,8 @@ protected function callPluginDynamic(array $matches) $arrOut = $this->buildRenderableArray($arr); + $arrInternal = $this->buildRenderableArray($this->internal); + // Block if (isset($matches[3])) { @@ -681,6 +686,7 @@ protected function callPluginDynamic(array $matches) internal = $arrInternal; echo \$class->render('$pluginContent', $arrOut); ?> OUTPUT; @@ -691,6 +697,7 @@ protected function callPluginDynamic(array $matches) internal = $arrInternal; echo \$class->render($arrOut); ?> OUTPUT; diff --git a/tests/SurfStack/Templating/Template_Engine_Test.php b/tests/SurfStack/Templating/Template_Engine_Test.php index ef4e0b7..69ca822 100644 --- a/tests/SurfStack/Templating/Template_Engine_Test.php +++ b/tests/SurfStack/Templating/Template_Engine_Test.php @@ -249,7 +249,7 @@ public function testConversion() public function testNoLoadPlugins() { - $this->expectOutputString("{BoldBlock name='world'}Hello{/BoldBlock}!"); + $this->expectOutputString("{Bold name='world'}Hello{/Bold}!"); $this->view->setTemplate(__DIR__.'/template/block.tpl'); diff --git a/tests/SurfStack/Templating/plugin/BoldBlock.php b/tests/SurfStack/Templating/plugin/Bold.php similarity index 85% rename from tests/SurfStack/Templating/plugin/BoldBlock.php rename to tests/SurfStack/Templating/plugin/Bold.php index 0535e70..45f0ed7 100644 --- a/tests/SurfStack/Templating/plugin/BoldBlock.php +++ b/tests/SurfStack/Templating/plugin/Bold.php @@ -2,7 +2,7 @@ namespace SurfStack\Templating\Plugin; -class BoldBlock extends Block +class Bold extends Block { function render($strContent, $arrData) { diff --git a/tests/SurfStack/Templating/plugin/TimeSlice.php b/tests/SurfStack/Templating/plugin/Time.php similarity index 79% rename from tests/SurfStack/Templating/plugin/TimeSlice.php rename to tests/SurfStack/Templating/plugin/Time.php index b90747c..98a826b 100644 --- a/tests/SurfStack/Templating/plugin/TimeSlice.php +++ b/tests/SurfStack/Templating/plugin/Time.php @@ -2,7 +2,7 @@ namespace SurfStack\Templating\Plugin; -class TimeSlice extends Slice +class Time extends Slice { function render($arrData) { diff --git a/tests/SurfStack/Templating/template/block.tpl b/tests/SurfStack/Templating/template/block.tpl index a5458ef..d726b48 100644 --- a/tests/SurfStack/Templating/template/block.tpl +++ b/tests/SurfStack/Templating/template/block.tpl @@ -1 +1 @@ -{BoldBlock name='world'}Hello{/BoldBlock}! \ No newline at end of file +{Bold name='world'}Hello{/Bold}! \ No newline at end of file diff --git a/tests/SurfStack/Templating/template/slice.tpl b/tests/SurfStack/Templating/template/slice.tpl index f05724a..feb31cb 100644 --- a/tests/SurfStack/Templating/template/slice.tpl +++ b/tests/SurfStack/Templating/template/slice.tpl @@ -1 +1 @@ -{TimeSlice} \ No newline at end of file +{Time} \ No newline at end of file