Skip to content

Commit 7c00ccf

Browse files
committed
UIMacros: own handler for {extends}
# Conflicts: # tests/Bridges.Latte/expected/UIMacros.snippet4.phtml
1 parent 54a11d2 commit 7c00ccf

File tree

6 files changed

+106
-1
lines changed

6 files changed

+106
-1
lines changed

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ public static function install(Latte\Compiler $compiler)
3636
$me->addMacro('plink', [$me, 'macroLink']);
3737
$me->addMacro('link', [$me, 'macroLink']);
3838
$me->addMacro('ifCurrent', [$me, 'macroIfCurrent'], '}'); // deprecated; use n:class="$presenter->linkCurrent ? ..."
39+
$me->addMacro('extends', [$me, 'macroExtends']);
40+
$me->addMacro('layout', [$me, 'macroExtends']);
41+
}
42+
43+
44+
/**
45+
* Initializes before template parsing.
46+
* @return void
47+
*/
48+
public function initialize()
49+
{
50+
$this->getCompiler()->addMethod('getParentName', '
51+
return !$this->getReferrer() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
52+
? $this->params["_control"]->findLayoutTemplateFile() : NULL;
53+
');
3954
}
4055

4156

@@ -103,6 +118,18 @@ public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
103118
}
104119

105120

121+
/**
122+
* {extends auto}
123+
*/
124+
public function macroExtends(MacroNode $node, PhpWriter $writer)
125+
{
126+
if ($node->modifiers || $node->parentNode || $node->args !== 'auto') {
127+
return FALSE;
128+
}
129+
$this->getCompiler()->addMethod('getParentName', 'return $this->params["_presenter"]->findLayoutTemplateFile();');
130+
}
131+
132+
106133
/** @deprecated */
107134
public static function renderSnippets(Nette\Application\UI\Control $control, \stdClass $local, array $params)
108135
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use Nette\Bridges\ApplicationLatte\UIMacros;
4+
use Tester\Assert;
5+
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
class MockPresenter extends Nette\Application\UI\Presenter
11+
{
12+
function findLayoutTemplateFile()
13+
{
14+
return 'layout.latte';
15+
}
16+
}
17+
18+
19+
$latte = new Latte\Engine;
20+
$latte->setLoader(new Latte\Loaders\StringLoader);
21+
UIMacros::install($latte->getCompiler());
22+
23+
$template = $latte->createTemplate('');
24+
$template->params['_control'] = new MockPresenter;
25+
Assert::same('layout.latte', $template->getParentName());
26+
27+
$template = $latte->createTemplate('{block}...{/block}');
28+
$template->params['_control'] = new MockPresenter;
29+
Assert::same('layout.latte', $template->getParentName());
30+
31+
$template = $latte->createTemplate('{block name}...{/block}');
32+
$template->params['_control'] = new MockPresenter;
33+
Assert::same('layout.latte', $template->getParentName());
34+
35+
$template = $latte->createTemplate('{extends "file.latte"} {block name}...{/block}');
36+
$template->params['_control'] = new MockPresenter;
37+
Assert::same('file.latte', $template->getParentName());
38+
39+
$template = $latte->createTemplate('{extends "file.latte"}');
40+
$template->params['_control'] = new MockPresenter;
41+
Assert::same('file.latte', $template->getParentName());
42+
43+
$template = $latte->createTemplate('{extends $file} {block name}...{/block}');
44+
$template->params['_control'] = new MockPresenter;
45+
$template->params['file'] = 'file.latte';
46+
Assert::same('file.latte', $template->getParentName());
47+
48+
$template = $latte->createTemplate('{extends none}');
49+
$template->params['_control'] = new MockPresenter;
50+
Assert::null($template->getParentName());
51+
52+
$template = $latte->createTemplate('{extends auto}');
53+
$template->params['_presenter'] = new MockPresenter;
54+
Assert::same('layout.latte', $template->getParentName());

tests/Bridges.Latte/expected/UIMacros.dynamicsnippets.alt.phtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ if ($this->tryRenderParent(get_defined_vars())) return ?>
2525
}
2626

2727

28+
function getParentName()
29+
{
30+
return %A%
31+
}
32+
33+
2834
function blockOuter1($_b, $_args)
2935
{
3036
extract($_args);

tests/Bridges.Latte/expected/UIMacros.dynamicsnippets.phtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ if ($this->tryRenderParent(get_defined_vars())) return ?>
2121
}
2222

2323

24+
function getParentName()
25+
{
26+
return %A%
27+
}
28+
29+
2430
function blockOuter($_b, $_args)
2531
{
2632
extract($_args);

tests/Bridges.Latte/expected/UIMacros.snippet.alt.phtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $_b)) return;
3131
}
3232

3333

34+
function getParentName()
35+
{
36+
return %A%
37+
}
38+
39+
3440
function blockOuter($_b, $_args)
3541
{
3642
extract($_args);

tests/Bridges.Latte/expected/UIMacros.snippet.phtml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $_b)) return;
4040
}
4141

4242

43-
function block_%h%($_b, $_args)
43+
function getParentName()
44+
{
45+
return %A%
46+
}
47+
48+
49+
function block_b14a7($_b, $_args)
4450
{
4551
extract($_args);
4652
$_control->redrawControl('', FALSE);

0 commit comments

Comments
 (0)