Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Created October 15, 2010 10:41
Show Gist options
  • Save fprochazka/627987 to your computer and use it in GitHub Desktop.
Save fprochazka/627987 to your computer and use it in GitHub Desktop.
LookoutControl - Control with fake life cycle
<div>{$result}</div>
<?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);
}
}
<?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));
}
}
{control myComponent:default, 'arg1'}
{control myComponent, 'arg1'}
{control myComponent:something, 'arg'}
<b>{$result}</b>
@fprochazka
Copy link
Author

poslední verze s opravami: https://github.com/Kdyby/Framework/blob/master/libs/Kdyby/Application/UI/LookoutControl.php tento gist už nebudu aktualizovat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment