Skip to content

Commit

Permalink
Section Else #175
Browse files Browse the repository at this point in the history
- Added ability for else parts in sections
- It supports stringables and closures for
  lazy evaluation

Signed-off-by: RJ Garcia <[email protected]>
  • Loading branch information
ragboyjr committed Jan 26, 2018
1 parent 2377d3f commit 519b902
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Extension/LayoutSections/LayoutSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function register(Plates\Engine $plates) {

return [
'layout' => [layoutFunc(), $template_args],
'section' => [sectionFunc(), $one_arg],
'section' => [sectionFunc(), RenderContext\assertArgsFunc(1, 1)],
'start' => [startFunc(), $one_arg],
'push' => [startFunc(START_APPEND), $one_arg],
'unshift' => [startFunc(START_PREPEND), $one_arg],
Expand Down
13 changes: 11 additions & 2 deletions src/Extension/LayoutSections/layout-sections.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace League\Plates\Extension\LayoutSections;

use League\Plates;
use League\Plates\Template;

use League\Plates\Extension\RenderContext\FuncArgs;
Expand All @@ -26,8 +27,16 @@ function layoutFunc() {

function sectionFunc() {
return function(FuncArgs $args) {
list($name) = $args->args;
return $args->template()->get('sections')->get($name);
list($name, $else) = $args->args;

$res = $args->template()->get('sections')->get($name);
if ($res || !$else) {
return $res;
}

return is_callable($else)
? Plates\Util\obWrap($else)
: (string) $else;
};
}

Expand Down
17 changes: 3 additions & 14 deletions src/RenderTemplate/PhpRenderTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,15 @@ public function renderTemplate(Plates\Template $template, Plates\RenderTemplate
$inc = self::createInclude();
$inc = $this->bind ? ($this->bind)($inc, $template) : $inc;

$cur_level = ob_get_level();
try {
return $inc($template->get('path'), $template->data);
} catch (\Exception $e) {}
catch (\Throwable $e) {}

// clean the ob stack
while (ob_get_level() > $cur_level) {
ob_end_clean();
}

throw $e;
return Plates\Util\obWrap(function() use ($inc, $template) {
$inc($template->get('path'), $template->data);
});
}

private static function createInclude() {
return function() {
ob_start();
extract(func_get_arg(1));
include func_get_arg(0);
return ob_get_clean();
};
}
}
20 changes: 20 additions & 0 deletions src/Util/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ function id() {
};
}

/** wraps a closure in output buffering and returns the buffered
content. */
function obWrap(callable $wrap) {
$cur_level = ob_get_level();

try {
ob_start();
$wrap();
return ob_get_clean();
} catch (\Exception $e) {}
catch (\Throwable $e) {}

// clean the ob stack
while (ob_get_level() > $cur_level) {
ob_end_clean();
}

throw $e;
}

/** simple utility that wraps php echo which allows for stubbing out the
echo func for testing */
function phpEcho() {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/fixtures/standard/layouts/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<html>
<head>
<title>Profile - <?=$name?></title>
<?=$v->section('meta', function() { ?>
<meta charset="UTF-8"/>
<?php }); ?>
</head>
<body>
<?php $v->component('../alerts', ['type' => 'success']) ?>
Expand Down
3 changes: 2 additions & 1 deletion test/integration/plates.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<html>
<head>
<title>Profile - RJ & Emily</title>
</head>
<meta charset="UTF-8"/>
</head>
<body>
<div class="alert success">
<p>Success!</p>
Expand Down
27 changes: 24 additions & 3 deletions test/unit/extension-layout-sections.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
START_PREPEND,
START_REPLACE
};
use function League\Plates\Extension\LayoutSections\{startFunc};
use function League\Plates\Extension\LayoutSections\{
startFunc,
sectionFunc
};
use function League\Plates\Extension\RenderContext\{endFunc};


Expand Down Expand Up @@ -80,8 +83,26 @@
xdescribe('layoutFunc', function() {
it('forks a template and sets the layout');
});
xdescribe('sectionFunc', function() {
it('gets a section from the template sections');
describe('sectionFunc', function() {
it('gets a section from the template sections', function() {
($this->template)()->get('sections')->add('foo', 'bar');
$res = sectionFunc()($this->args->withArgs(['foo', null]));
expect($res)->equal('bar');
});
it('returns an empty section if no else is defined', function() {
$res = sectionFunc()($this->args->withArgs(['foo', null]));
expect($res)->equal(null);
});
it('returns the else content if no section is defined', function() {
$res = sectionFunc()($this->args->withArgs(['foo', 'baz']));
expect($res)->equal('baz');
});
it('invokes the else callable if no section is defined', function() {
$res = sectionFunc()($this->args->withArgs(['foo', function() {
echo 'baz';
}]));
expect($res)->equal('baz');
});
});
describe('startFunc', function() {
$create_test = function($update_text, $update, $expected) {
Expand Down

0 comments on commit 519b902

Please sign in to comment.