Skip to content

Commit 9d71d64

Browse files
committed
UIMacros: own handler for {extends}
1 parent 72efcd7 commit 9d71d64

6 files changed

Lines changed: 114 additions & 21 deletions

File tree

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('getExtends', '
51+
return !$this->getParent() && $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('getExtends', '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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
$template->initialize(NULL);
26+
Assert::same('layout.latte', $template->getExtends());
27+
28+
$template = $latte->createTemplate('{block}...{/block}');
29+
$template->params['_control'] = new MockPresenter;
30+
$template->initialize(NULL);
31+
Assert::same('layout.latte', $template->getExtends());
32+
33+
$template = $latte->createTemplate('{block name}...{/block}');
34+
$template->params['_control'] = new MockPresenter;
35+
$template->initialize(NULL);
36+
Assert::same('layout.latte', $template->getExtends());
37+
38+
$template = $latte->createTemplate('{extends "file.latte"} {block name}...{/block}');
39+
$template->params['_control'] = new MockPresenter;
40+
$template->initialize(NULL);
41+
Assert::same('file.latte', $template->getExtends());
42+
43+
$template = $latte->createTemplate('{extends "file.latte"}');
44+
$template->params['_control'] = new MockPresenter;
45+
$template->initialize(NULL);
46+
Assert::same('file.latte', $template->getExtends());
47+
48+
$template = $latte->createTemplate('{extends $file} {block name}...{/block}');
49+
$template->params['_control'] = new MockPresenter;
50+
$template->params['file'] = 'file.latte';
51+
$template->initialize(NULL);
52+
Assert::same('file.latte', $template->getExtends());
53+
54+
$template = $latte->createTemplate('{extends none}');
55+
$template->params['_control'] = new MockPresenter;
56+
$template->initialize(NULL);
57+
Assert::null($template->getExtends());
58+
59+
$template = $latte->createTemplate('{extends auto}');
60+
$template->params['_presenter'] = new MockPresenter;
61+
$template->initialize(NULL);
62+
Assert::same('layout.latte', $template->getExtends());

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ if (Latte\Macros\BlockMacrosRuntime::progress($this, get_defined_vars())) return
2525
}
2626

2727

28+
function getExtends()
29+
{
30+
return %A%
31+
}
32+
33+
2834
function blockOuter1($_b, $_args)
2935
{
3036
unset($_args["this"]); foreach ($_args as $__k => $__v) $$__k = $__v;
@@ -54,9 +60,4 @@ $_control->redrawControl('outer2', FALSE);
5460
<?php if (isset($_l->dynSnippets)) return $_l->dynSnippets;
5561
}
5662

57-
58-
function getExtends()
59-
{
60-
}
61-
6263
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ if (Latte\Macros\BlockMacrosRuntime::progress($this, get_defined_vars())) return
2121
}
2222

2323

24+
function getExtends()
25+
{
26+
return %A%
27+
}
28+
29+
2430
function blockOuter($_b, $_args)
2531
{
2632
unset($_args["this"]); foreach ($_args as $__k => $__v) $$__k = $__v;
@@ -38,9 +44,4 @@ $_control->redrawControl('outer', FALSE);
3844
<?php if (isset($_l->dynSnippets)) return $_l->dynSnippets;
3945
}
4046

41-
42-
function getExtends()
43-
{
44-
}
45-
4647
}

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

Lines changed: 6 additions & 5 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 getExtends()
35+
{
36+
return %A%
37+
}
38+
39+
3440
function blockOuter($_b, $_args)
3541
{
3642
unset($_args["this"]); foreach ($_args as $__k => $__v) $$__k = $__v;
@@ -57,9 +63,4 @@ $_control->redrawControl('gallery', FALSE);
5763

5864
}
5965

60-
61-
function getExtends()
62-
{
63-
}
64-
6566
}

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

Lines changed: 7 additions & 6 deletions
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 getExtends()
44+
{
45+
return %A%
46+
}
47+
48+
49+
function block_b14a7($_b, $_args)
4450
{
4551
unset($_args["this"]); foreach ($_args as $__k => $__v) $$__k = $__v;
4652
$_control->redrawControl('', FALSE);
@@ -83,9 +89,4 @@ $_control->redrawControl('title2', FALSE)
8389
?>Title 2<?php
8490
}
8591

86-
87-
function getExtends()
88-
{
89-
}
90-
9192
}

0 commit comments

Comments
 (0)