Permalink
Browse files

Merge pull request #212 from thephpleague/section-else

Section Else #175
  • Loading branch information...
ragboyjr committed Jan 26, 2018
2 parents 2377d3f + b478ae5 commit b3978b0ef885696e1a5f4b8f926911ce0dde14b2
@@ -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],
@@ -2,6 +2,7 @@
namespace League\Plates\Extension\LayoutSections;
use League\Plates;
use League\Plates\Template;
use League\Plates\Extension\RenderContext\FuncArgs;
@@ -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;
};
}
@@ -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();
};
}
}
View
@@ -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() {
@@ -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']) ?>
@@ -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>
@@ -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};
@@ -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) {

0 comments on commit b3978b0

Please sign in to comment.