Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This file is part of the Kdyby (http://www.kdyby.org)
- *
- * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
- *
- * @license http://www.kdyby.org/license
- */
- namespace Kdyby\Components\Header;
- use Kdyby;
- use Kdyby\Templates\LatteHelpers;
- use Nette;
- use Nette\Application\UI;
- use Nette\Latte;
- use Nette\Templating\Template;
- /**
- * @author Filip Procházka <[email protected]>
- */
- class HeadMacro extends Nette\Object implements Latte\IMacro
- {
- /**
- * @param \Nette\Latte\Parser $parser
- */
- public static function install(Latte\Parser $parser)
- {
- $parser->addMacro('head', new static());
- }
- /**
- * Initializes before template parsing.
- * @return void
- */
- public function initialize()
- {
- }
- /**
- * Finishes template parsing.
- * @return array(prolog, epilog)
- */
- public function finalize()
- {
- }
- /**
- * @param \Nette\Latte\MacroNode $node
- *
- * @return bool|string
- */
- public function nodeOpened(Latte\MacroNode $node)
- {
- return Latte\PhpWriter::using($node)
- ->write('<?php Kdyby\Components\Header\HeadMacro::headBegin($presenter); ?>');
- }
- /**
- * @param \Nette\Latte\MacroNode $node
- *
- * @return string
- */
- public function nodeClosed(Latte\MacroNode $node)
- {
- $writer = Latte\PhpWriter::using($node);
- $node->content = $this->wrapTags(Template::optimizePhp($node->content), $writer);
- return $writer->write('<?php Kdyby\Components\Header\HeadMacro::headEnd($presenter); ?>');
- }
- /**
- * @param string $content
- *
- * @return string
- */
- private function wrapTags($content, Latte\PhpWriter $writer)
- {
- $code = NULL;
- foreach ($parts = LatteHelpers::splitPhp($content) as $item) {
- if (substr($item, 0, 5) === '<?php') {
- $code .= $item;
- continue;
- }
- $code .= Nette\Utils\Strings::replace($item, array(
- '~<([^<\s]+)\s+~' => $writer->write('<?php ob_start(); ?>') . '<\\1 ',
- '~\s+/>~' => " />" . $writer->write('<?php Kdyby\Components\Header\HeadMacro::tagCaptureEnd($presenter); ?>'),
- ));
- }
- echo "<hr>";
- echo nl2br(htmlspecialchars($code));
- echo "<hr>";
- return $content;
- }
- /**
- * @param \Nette\Application\UI\Presenter $presenter
- */
- public static function headBegin(UI\Presenter $presenter)
- {
- $head = static::getHead($presenter);
- echo $head->getElement()->startTag();
- $head->renderContent();
- }
- /**
- * @param \Nette\Application\UI\Presenter $presenter
- */
- public static function headEnd(UI\Presenter $presenter)
- {
- $head = static::getHead($presenter);
- echo $head->getElement()->startTag();
- }
- /**
- * @param \Nette\Application\UI\Presenter $presenter
- */
- public static function tagCaptureEnd(UI\Presenter $presenter)
- {
- $head = static::getHead($presenter);
- $tag = ob_end_clean();
- dump($tag);
- }
- /**
- * @param \Nette\ComponentModel\Container $component
- * @return \Kdyby\Components\Header\HeaderControl
- */
- private static function getHead(Nette\ComponentModel\Container $component)
- {
- if (!$component->getComponent('head', FALSE)) {
- throw new Kdyby\InvalidStateException('You have to register Kdyby\Components\Header\HeaderControl as presenter component named "head".');
- }
- return $component->getComponent('head');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment