wikipoints/classes/Controller.php

92 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
abstract class Controller
{
static $layout = 'layouts/default.php';
static private $styles = array();
static private $scripts = array();
static private $og = array();
static private $vars = 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;
}
}
static function addVar($name, $value)
{
self::$vars[$name] = $value;
}
/**
* Рендерит шаблон без лейаута.
* @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,
'vars' => self::$vars,
'data' => $data,
), true);
if ($return) {
return $result;
} else {
echo $result;
return true;
}
}
}