Created
October 15, 2010 10:41
-
-
Save fprochazka/627987 to your computer and use it in GitHub Desktop.
LookoutControl - Control with fake life cycle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div>{$result}</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Kdyby\Control; | |
use Nette; | |
use Nette\String; | |
class LookoutControl extends Nette\Application\Control | |
{ | |
/** @var string */ | |
private $view; | |
/** @var array */ | |
private $renderParams = array(); | |
/** @var array */ | |
private static $methods = array(); | |
/** | |
* @return array | |
*/ | |
public function getRenderParams() | |
{ | |
return $this->renderParams; | |
} | |
/** | |
* @return string | |
*/ | |
public function getView() | |
{ | |
return $this->view; | |
} | |
/** | |
* @param string $type | |
* @param mixed $param | |
* @return string | |
*/ | |
final public function render($type = NULL, $param = NULL) | |
{ | |
$class = get_class($this); | |
if (!isset(self::$methods[$class])) { | |
self::$methods[$class] = get_class_methods($this); | |
} | |
$this->view = $this->view ?: 'default'; | |
$this->renderParams = $this->renderParams ?: func_get_args(); | |
$viewMethod = 'view' . ucfirst($this->view); | |
if (in_array('beforeRender', self::$methods[$class])) { | |
call_user_func_array(array($this, 'beforeRender'), $this->renderParams); | |
} | |
$dir = dirname($this->reflection->fileName); | |
$view = lcfirst($this->view); | |
$templates = array( | |
$dir . '/' . $view . '.latte', | |
$dir . '/' . $view . '.phtml' | |
); | |
foreach ($templates as $file){ | |
if (file_exists($file)) { | |
$this->template->setFile($file); | |
break; | |
} | |
} | |
ob_start(); | |
call_user_func_array(array($this, $viewMethod), $this->renderParams); | |
$output = ob_get_clean(); | |
if (!$output && file_exists($file)) { // raw output from function | |
$output = (string)$this->template; | |
} | |
echo $output; | |
if (in_array('afterRender', self::$methods[$class])) { | |
call_user_func_array(array($this, 'afterRender'), $this->renderParams); | |
} | |
$this->view = NULL; | |
$this->renderParams = array(); | |
} | |
/** | |
* Calls self::render($view, $args) instead of nonexisting render<view>($args) methods | |
* @param string $method | |
* @param array $args | |
* @return mixed | |
*/ | |
public function __call($method, $args) | |
{ | |
if (String::startsWith($method, 'render')) { | |
$this->view = substr($method, 6); | |
$this->renderParams = $args; | |
return call_user_func(array($this, 'render')); | |
} | |
return parent::__call($method, $args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyComponent extends LookoutControl | |
{ | |
protected function beforeRender($arg1, $arg2 = NULL, $arg3 = NULL) | |
{ | |
$this->something($arg1 + $this->blablaMethod($arg2, $arg3)); | |
} | |
public function viewDefault($arg1, $arg2 = NULL, $arg3 = NULL) | |
{ | |
$this->template->result = $arg1 + $this->blablaMethod($arg2, $arg3); | |
} | |
public function viewSomething($arg) | |
{ | |
$this->template->result = $this->blahMethod($arg); | |
} | |
protected function afterRender($arg1, $arg2 = NULL, $arg3 = NULL) | |
{ | |
$this->gnihtemos($arg1 + $this->blablaMethod($arg2, $arg3)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{control myComponent:default, 'arg1'} | |
{control myComponent, 'arg1'} | |
{control myComponent:something, 'arg'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<b>{$result}</b> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
poslední verze s opravami: https://github.com/Kdyby/Framework/blob/master/libs/Kdyby/Application/UI/LookoutControl.php tento gist už nebudu aktualizovat