Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

Commit

Permalink
- Created latte macro
Browse files Browse the repository at this point in the history
- Added ability to change decorator
  • Loading branch information
akadlec committed Dec 9, 2016
1 parent e088fc4 commit e526447
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/IPub/Widgets/Components/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use IPub\Widgets\Exceptions;
use IPub\Widgets\Managers;
use IPub\Widgets\Widgets;
use Tracy\Debugger;

/**
* Widgets container control definition
Expand Down Expand Up @@ -135,6 +136,13 @@ protected function attached($presenter)
*/
public function render()
{
foreach (func_get_args() as $arg) {
// Check if decorator name is provided
if (is_string($arg)) {
$this->setDecorator($arg);
}
}

// Check if control has template
if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
// Assign vars to template
Expand Down Expand Up @@ -174,6 +182,12 @@ public function setDecorator(string $decorator)
{
// Try to find decorator factory
if ($factory = $this->decoratorsManager->get($decorator)) {
// Check if some decorator is registered...
if ($component = $this->getComponent('decorator', FALSE)) {
// ... if yes, remove it
$this->removeComponent($component);
}

// Register decorator component
$this->addComponent($factory->create(), 'decorator');

Expand Down
4 changes: 4 additions & 0 deletions src/IPub/Widgets/DI/WidgetsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public function beforeCompile()
}
}
}

// Install extension latte macros
$latteFactory = $builder->getDefinition($builder->getByType(Nette\Bridges\ApplicationLatte\ILatteFactory::class) ?: 'nette.latteFactory');
$latteFactory->addSetup('IPub\Widgets\Latte\Macros::install(?->getCompiler())', ['@self']);
}

/**
Expand Down
98 changes: 98 additions & 0 deletions src/IPub/Widgets/Latte/Macros.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Macros.php
*
* @copyright More in license.md
* @license http://www.ipublikuj.eu
* @author Adam Kadlec http://www.ipublikuj.eu
* @package iPublikuj:Widgets!
* @subpackage Latte
* @since 1.0.0
*
* @date 10.10.14
*/

declare(strict_types = 1);

namespace IPub\Widgets\Latte;

use Nette;
use Nette\Bridges;
use Nette\Utils;

use Latte;
use Latte\Compiler;
use Latte\MacroNode;
use Latte\Macros\MacroSet;
use Latte\PhpWriter;

/**
* Permissions latte macros definition
*
* @package iPublikuj:Widgets!
* @subpackage Latte
*
* @author Adam Kadlec <[email protected]>
*/
final class Macros extends Bridges\ApplicationLatte\UIMacros
{
/**
* @param Compiler $compiler
*
* @return void
*/
public static function install(Compiler $compiler)
{
$me = new static($compiler);

/**
* {widget positionName}
*/
$me->addMacro('widgets', [$me, 'macroWidgets']);
}

/**
* @param MacroNode $node
* @param PhpWriter $writer
*
* @return string
*
* @throws Latte\CompileException
*/
public static function macroWidgets(MacroNode $node, PhpWriter $writer) : string
{
$words = $node->tokenizer->fetchWords();
if (!$words) {
throw new Latte\CompileException('Missing widgets position name in {widgets}');
}
$name = $writer->formatWord($words[0]);
$method = isset($words[1]) ? ucfirst($words[1]) : '';
$method = Utils\Strings::match($method, '#^\w*\z#') ? "render$method" : "{\"render$method\"}";

$tokens = $node->tokenizer;
$pos = $tokens->position;
$param = $writer->formatArray();
$tokens->position = $pos;
while ($tokens->nextToken()) {
if ($tokens->isCurrent('=>') && !$tokens->depth) {
$wrap = TRUE;
break;
}
}
if (empty($wrap)) {
$param = substr($param, 1, -1); // removes array() or []
}
$prefix = '';
if (is_string($name) && strpos($name, '-') === FALSE) {
$prefix = '"widgets-" .';
}
return "/* line $node->startLine */ "
. ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '')
. '$_tmp = $this->global->uiControl->getComponent('. $prefix . $name . '); '
. 'if ($_tmp instanceof Nette\Application\UI\IRenderable) $_tmp->redrawControl(NULL, FALSE); '
. ($node->modifiers === ''
? "\$_tmp->$method($param);"
: $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean());")
);
}
}
9 changes: 8 additions & 1 deletion src/IPub/Widgets/Widgets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use IPub\Widgets\Decorators;
use IPub\Widgets\Entities;
use IPub\Widgets\Exceptions;
use Tracy\Debugger;

/**
* Widgets control definition
Expand Down Expand Up @@ -172,8 +173,14 @@ protected function beforeRender()
$this->template->setTranslator($this->getTranslator());
}

$templateFile = $this->getTemplate()->getFile();

if (is_callable($templateFile)) {
$templateFile = call_user_func($templateFile);
}

// If template was not defined before...
if ($this->template->getFile() === NULL) {
if ($templateFile === NULL) {
// Get component actual dir
$dir = dirname($this->getReflection()->getFileName());

Expand Down

0 comments on commit e526447

Please sign in to comment.