Skip to content

Commit

Permalink
Latte: support for single block rendering [Closes #101]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 28, 2016
1 parent 87a79a9 commit 56e4514
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/Latte/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function __construct()
* Renders template to output.
* @return void
*/
public function render($name, array $params = [])
public function render($name, array $params = [], $block = NULL)
{
$this->createTemplate($name)
->setParameters($params)
->setParameters($params + ['_block' => $block])
->render();
}

Expand All @@ -76,11 +76,11 @@ public function render($name, array $params = [])
* Renders template to string.
* @return string
*/
public function renderToString($name, array $params = [])
public function renderToString($name, array $params = [], $block = NULL)
{
ob_start(function () {});
try {
$this->render($name, $params);
$this->render($name, $params, $block);
} catch (\Throwable $e) {
ob_end_clean();
throw $e;
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Macros/BlockMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function finalize()
}
$compiler->addProperty('blocks', array_merge_recursive($functions, $this->blockTypes));

$prolog = 'Latte\Macros\BlockMacrosRuntime::initialize($this, $_b)';
$prolog = 'if (Latte\Macros\BlockMacrosRuntime::initialize($this, $_b)) return;';
if (!$this->namedBlocks) {
$epilog = 'if (Latte\Macros\BlockMacrosRuntime::progress($this, get_defined_vars())) return;';
}
Expand Down
9 changes: 7 additions & 2 deletions src/Latte/Macros/BlockMacrosRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BlockMacrosRuntime


/**
* @return void
* @return bool
*/
public static function initialize(Latte\Template $template, \stdClass $blocks)
{
Expand All @@ -29,8 +29,13 @@ public static function initialize(Latte\Template $template, \stdClass $blocks)
self::checkType($info[1], $blocks->types, $name);
}

if ($template->params['_l']->extends = $template->getExtends()) {
$params = $template->params;
if ($params['_l']->extends = $template->getExtends()) {
ob_start(function () {});

} elseif (!empty($params['_block']) && empty($params['_g']->includingBlock)) {
self::callBlock($blocks, $params['_block'], $params);
return TRUE;
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Latte/Engine.renderBlock.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;

Assert::match(
'This is definition #5',
trim($latte->renderToString(__DIR__ . '/templates/defineblock.latte', ['var' => 5], 'test'))
);

Assert::match(
'Homepage | My websiteMy website',
$latte->renderToString(__DIR__ . '/templates/inheritance.child1.latte', [], 'title')
);

0 comments on commit 56e4514

Please sign in to comment.