92 lines
2.1 KiB
PHP
92 lines
2.1 KiB
PHP
<?php
|
||
|
||
abstract class Controller
|
||
{
|
||
|
||
static $layout = 'layout.php';
|
||
static private $styles = array();
|
||
static private $scripts = array();
|
||
|
||
|
||
static function addStyle($name)
|
||
{
|
||
if (array_search($name, self::$styles) === FALSE )
|
||
{
|
||
self::$styles[] = $name;
|
||
}
|
||
}
|
||
|
||
static function addScript($name)
|
||
{
|
||
if (array_search($name, self::$scripts) === FALSE )
|
||
{
|
||
self::$scripts[] = $name;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Рендерит шаблон без лейаута.
|
||
* @param string $template - имя шаблона
|
||
* @param array $data - передаваемые параметры
|
||
* @param bool $return - вернуть результат в виде строки
|
||
* @return string
|
||
*/
|
||
static function renderPartial($template, $data, $return = false)
|
||
{
|
||
if (!file_exists(App::getBasedir() . 'views/' . $template))
|
||
return 'Template not found';
|
||
|
||
if ($return)
|
||
{
|
||
$oldContentFormBuffer_temp = ob_get_clean();
|
||
ob_start();
|
||
}
|
||
|
||
if ( $data && is_array($data) )
|
||
{
|
||
extract($data);
|
||
}
|
||
|
||
include App::getBasedir() . 'views/' . $template;
|
||
|
||
if ($return)
|
||
{
|
||
$newContentFormBuffer_temp = ob_get_clean();
|
||
ob_start();
|
||
echo $oldContentFormBuffer_temp;
|
||
return $newContentFormBuffer_temp;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* Рендерит шаблон и вписывает его в лейаут.
|
||
* @param string $template - имя шаблона
|
||
* @param array $data - передаваемые параметры
|
||
* @param bool $return - вернуть результат в виде строки
|
||
* @return string
|
||
*/
|
||
static function render($template, $data, $return = false)
|
||
{
|
||
$content = self::renderPartial($template, $data, true);
|
||
|
||
$result = self::renderPartial(self::$layout, array(
|
||
'content' => $content,
|
||
'styles' => self::$styles,
|
||
'scripts' => self::$scripts,
|
||
'data' => $data,
|
||
), true);
|
||
|
||
if ($return)
|
||
{
|
||
return $result;
|
||
} else
|
||
{
|
||
echo $result;
|
||
return true;
|
||
}
|
||
}
|
||
|
||
}
|