Files
DomFramework/outputhtml.php

113 lines
3.8 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
require_once ("output.php");
/** Display in HTML the datas provided, with the layout support */
class outputhtml extends output
{
/** Data is printed by viewClass->viewmethod, in the middle of $layout
title is put in the title of the HTML page
$replacement modify the result (it can do title too :
array ("{title}"=>"title to display")
@param mixed $data Data to display on the page
@param string|null $title Title to put on head of page
@param string|null $viewClass Class in views to use to display
@param string|null $viewMethod Method in the class in views
@param string|null $layout Layout file in views
@param array|null $replacement Replace the {key}=>value
@param array|null $variable PHP variables send to the view and to layout
(can be processed by foreach, if...) */
public function out ($data, $title = FALSE,
$viewClass = FALSE, $viewMethod = FALSE,
$layout = FALSE, $replacement = array(),
$variable = array (), $module = "")
{
@header("Cache-Control: no-store, no-cache, must-revalidate");
@header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
@header('Last-Modified: '.gmdate ('D, d M Y H:i:s').' GMT');
@header('Cache-Control: post-check=0, pre-check=0', false);
@header('Pragma: no-cache');
@header ("Content-Type: text/html");
$resView = $data;
if ($viewClass !== FALSE && $viewMethod !== FALSE)
{
if (! class_exists ($viewClass))
{
if ($module !== "")
require_once ("modules/$module/views/$viewClass.php");
else
require_once ("views/$viewClass.php");
}
$obj = new $viewClass;
$resView = $obj->$viewMethod ($data, $variable);
if (is_array ($resView))
{
if (isset ($resView["title"]))
$title = $resView["title"];
if (! isset ($resView["content"]))
throw new Exception (
_("No data provided from view $viewClass::$viewMethod"),
500);
$resView = $resView["content"];
}
}
if ($layout !== FALSE)
{
if (! file_exists ($layout) && file_exists ("views/$layout.html"))
$layout = "views/$layout.html";
$layoutPage = $this->layoutVariables ($layout, $variable);
$resView = str_replace ("{content}", $resView, $layoutPage);
}
else
{
// No layout : display a very basic HTML page
$layoutPage = <<< EOT
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>{title}</title>
</head>
<body>
{flash}
{content}
</body>
</html>
EOT;
$resView = str_replace ("{content}", $resView, $layoutPage);
}
// Do the title replacement in the replacement structure
if (! isset ($replacement["{title}"]))
$replacement["{title}"] = $title;
if (! isset ($replacement["{flash}"]))
$replacement["{flash}"] = "";
foreach ($replacement as $key=>$val)
$resView = str_replace ($key, $val, $resView);
echo $resView;
}
/** Get the layout and provide it the variables. The variables will be push in
global to the layout (they can be used like $XX)
@param string the layout file to load
@param array $variables The variables array to push to the layout
@return string the Layout with variables interpreted */
private function layoutVariables ($layout, $variables)
{
// The layout can be a external layout file or the HTML page itself.
// FIXME : Allow to manage variables in a layout provided in the variable,
// without eval use
if (! file_exists ($layout))
return $layout;
extract ($variables);
ob_start();
require ($layout);
return ob_get_clean ();
}
}