Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Remove plugin name dependency, updated plugin parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed May 3, 2014
1 parent 07a59fc commit e651172
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 29 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
6 changes: 6 additions & 0 deletions src/SurfStack/Templating/Plugin/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/SurfStack/Templating/Plugin/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 26 additions & 19 deletions src/SurfStack/Templating/Template_Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class Template_Engine
*/
function __construct($template)
{
$this->setTemplate($template);

// Set the default settings
$this->internal = array(
'StripTags' => false,
Expand All @@ -55,6 +53,8 @@ function __construct($template)
'CacheLifetime' => 3600,
'AlwaysCheckOriginal' => false,
);

$this->setTemplate($template);
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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)), '\'\"');
}
}

Expand All @@ -672,6 +675,8 @@ protected function callPluginDynamic(array $matches)

$arrOut = $this->buildRenderableArray($arr);

$arrInternal = $this->buildRenderableArray($this->internal);

// Block
if (isset($matches[3]))
{
Expand All @@ -681,6 +686,7 @@ protected function callPluginDynamic(array $matches)
<?php
\$plugin = '\SurfStack\Templating\Plugin\\\'.'$pluginName';
\$class = new \$plugin();
\$class->internal = $arrInternal;
echo \$class->render('$pluginContent', $arrOut);
?>
OUTPUT;
Expand All @@ -691,6 +697,7 @@ protected function callPluginDynamic(array $matches)
<?php
\$plugin = '\SurfStack\Templating\Plugin\\\'.'$pluginName';
\$class = new \$plugin();
\$class->internal = $arrInternal;
echo \$class->render($arrOut);
?>
OUTPUT;
Expand Down
2 changes: 1 addition & 1 deletion tests/SurfStack/Templating/Template_Engine_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SurfStack\Templating\Plugin;

class BoldBlock extends Block
class Bold extends Block
{
function render($strContent, $arrData)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SurfStack\Templating\Plugin;

class TimeSlice extends Slice
class Time extends Slice
{
function render($arrData)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/SurfStack/Templating/template/block.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{BoldBlock name='world'}Hello{/BoldBlock}!
{Bold name='world'}Hello{/Bold}!
2 changes: 1 addition & 1 deletion tests/SurfStack/Templating/template/slice.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{TimeSlice}
{Time}

0 comments on commit e651172

Please sign in to comment.