Skip to content

Commit 4317004

Browse files
committed
UIMacros: added macro {templatePrint}
1 parent 153409e commit 4317004

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Bridges\ApplicationLatte;
11+
12+
use Latte;
13+
use Latte\Runtime\TemplatePrinter as Helpers;
14+
use Nette\Application\UI\Presenter;
15+
16+
17+
/**
18+
* Generates blueprint of template class.
19+
*/
20+
final class TemplatePrinter
21+
{
22+
use Nette\SmartObject;
23+
24+
public function print(Latte\Runtime\Template $template, string $class = null): string
25+
{
26+
$types = array_map([Helpers::class, 'getType'], $template->getParameters());
27+
if ($template->getParameter('control') instanceof Presenter) {
28+
unset($types['control']);
29+
$subject = $template->getParameter('presenter');
30+
$class = $class ?: preg_replace('#Presenter$#', '', get_class($subject)) . ucfirst($subject->getView()) . 'Template';
31+
} else {
32+
unset($types['presenter']);
33+
$subject = $template->getParameter('control');
34+
$class = $class ?: preg_replace('#Control$#', '', get_class($subject)) . 'Template';
35+
}
36+
unset($types['user'], $types['baseUrl'], $types['basePath'], $types['flashes']);
37+
38+
$funcs = [];
39+
foreach ($template->global as $name => $val) {
40+
if (substr($name, 0, 3) === '_fn') {
41+
$name = substr($name, 3);
42+
$funcs[$name] = Helpers::printFunction($name, $val, true);
43+
}
44+
}
45+
unset($funcs['islinkcurrent'], $funcs['ismodulecurrent']);
46+
47+
$parts = explode('\\', $class);
48+
$name = array_pop($parts);
49+
$namespace = implode('\\', $parts);
50+
$parent = Template::class;
51+
$subject = explode('\\', get_class($subject));
52+
$subject = end($subject);
53+
return
54+
($namespace ? "namespace $namespace;\n\n" : '')
55+
. "class $name extends $parent\n"
56+
. "{\n" . Helpers::printProperties($types, true) . "\n}\n"
57+
. "\n\n"
58+
. "/**\n" . Helpers::printProperties($types, false) . "\n"
59+
. implode("\n", $funcs) . "\n */\n"
60+
. "class $name extends $parent\n"
61+
. "{\n}\n"
62+
. "\n\n"
63+
. "/**\n * @property $name \$template\n */\n"
64+
. "class $subject\n"
65+
. "{\n}\n";
66+
}
67+
}

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static function install(Latte\Compiler $compiler): void
4444
$me->addMacro('extends', [$me, 'macroExtends']);
4545
$me->addMacro('layout', [$me, 'macroExtends']);
4646
$me->addMacro('nonce', null, null, 'echo $this->global->uiNonce ? " nonce=\"{$this->global->uiNonce}\"" : "";');
47+
$me->addMacro('templatePrint', [$me, 'macroTemplatePrint'], null, null, self::ALLOWED_IN_HEAD);
4748
}
4849

4950

@@ -147,4 +148,14 @@ public function macroExtends(MacroNode $node, PhpWriter $writer)
147148
}
148149
$this->extends = $writer->write('$this->parentName = $this->global->uiPresenter->findLayoutTemplateFile();');
149150
}
151+
152+
153+
/**
154+
* {templatePrint [ClassName]}
155+
*/
156+
public function macroTemplatePrint(MacroNode $node, PhpWriter $writer)
157+
{
158+
$class = $node->tokenizer->fetchWord() ?: null;
159+
return $writer->write('ob_end_clean(); header("Content-Type: text/plain"); echo (new Nette\Bridges\ApplicationLatte\TemplatePrinter)->print($this, %var); exit;', $class);
160+
}
150161
}

0 commit comments

Comments
 (0)