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

Commit

Permalink
Updated extend to apply plugins before render
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed May 4, 2014
1 parent 4153002 commit 5d30f6e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
47 changes: 46 additions & 1 deletion src/SurfStack/Templating/Template_Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ function __construct($path, $template)

$this->setTemplateDir($path);
$this->setTemplate($template);

$this->setInternal('engine', $this);
}

/**
Expand Down Expand Up @@ -787,7 +789,8 @@ protected function dynamicPluginReplacement($matches)
\$class->store('arrEngineVariables', \$this->variables);
\$class->store('arrEngineInternals', \$this->internal);
\$class->store('arrPluginVariables', $sPassed);
echo \$class->render($pluginContent); ?>";
echo \$class->render($pluginContent); ?>
";
}

/**
Expand All @@ -801,6 +804,48 @@ protected function parsePlugins($content)
return preg_replace_callback(array_values($this->loadPlugins()), array($this, 'dynamicPluginReplacement') , $content);
}

/**
* Return the rendered outpu of a string run through the plugins
* @param string $content
* @return string
*/
function getRenderPlugins($content)
{
$file = tempnam($this->getCompileDir(), 'tmp');

file_put_contents($file, $this->parsePlugins($content));

ob_start();
require $file;
$output = ob_get_contents();
ob_end_clean();

unlink($file);

return $output;
}

/**
* Return the rendered output of a string run through the entire process
* @param string $content
* @return string
*/
function getRenderString($content)
{
$file = tempnam($this->getCompileDir(), 'tmp');

file_put_contents($file, $this->modifyTemplateRegex($content));

ob_start();
require $file;
$output = ob_get_contents();
ob_end_clean();

unlink($file);

return $output;
}

/**
* Replace custom tags with standard PHP tags
* @param string $content
Expand Down
5 changes: 3 additions & 2 deletions src/SurfStack/Templating/plugin/Extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace SurfStack\Templating\Plugin;
use SurfStack\Templating\Core\Slice;
use SurfStack\Templating\Template_Engine;

/**
* SurfStack Template Extend Block
Expand Down Expand Up @@ -97,8 +98,8 @@ function render()
}
}
while ($this->parents);
return $this->overlayTemplate();

return $this->arrEngineInternals['engine']->getRenderPlugins($this->overlayTemplate());
}
else
{
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 @@ -397,7 +397,7 @@ public function testExtend()

$content = str_replace("\r\n","\n", $this->output);

$this->assertSame($content, "<p>Hello world!</p>\n\n<div>\nTest from grandparent\nTest from parent\n</div>\n\n<div>\nSafe from child\nSafe from grandparent\n</div>");
$this->assertSame($content, "<p>Hello world!</p>\n\n<div>\nTest from grandparent\nTest from parent\n</div>\n\n<div>\nSafe from child ".date('Y')."\nSafe from grandparent\n</div>");
}

public function testNoCacheTemplates()
Expand Down
5 changes: 3 additions & 2 deletions tests/SurfStack/Templating/plugin/Extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace SurfStack\Templating\Plugin;
use SurfStack\Templating\Core\Slice;
use SurfStack\Templating\Template_Engine;

/**
* SurfStack Template Extend Block
Expand Down Expand Up @@ -97,8 +98,8 @@ function render()
}
}
while ($this->parents);
return $this->overlayTemplate();

return $this->arrEngineInternals['engine']->getRenderPlugins($this->overlayTemplate());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion tests/SurfStack/Templating/template/child.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{extend file='parent.tpl'}

{section name='safe'}
Safe from child
Safe from child {Time}
{parent}
{/section}

0 comments on commit 5d30f6e

Please sign in to comment.