Follow along with the video below to see how to install our site as a web app on your home screen.
Бележка: This feature may not be available in some browsers.
<html>
<head>
<title>Template by StormBreaker</title>
</head>
<body>
<?php
require_once 'Template.php'; // Вмъкваме си дефиницията за класа
$t = new Template("Templates/index.php"); // Първия параметър е пътя към темплейт файла. Ако сложите втория параметър на true тогава ще се включи изходния буфер, който се изключва при показване на темплейта или при ползване на $t->StopOutputBuffer();
// Ако е включен всичко след тази функция няма да се покаже на потребителя, а ще се запази в променлива, която може да се покаже в самия темплейт чрез $this->OutputBuffer
// Добавяме променлива в темплейта
$title = "Пробен темплейт";
$t->AddVar("title", $title);
$title = "друго заглавие"; // Променливата в темплейта си остава = "Пробен темплейт"
// Или я добавяме като референция
$title = "Пробен темплейт";
$t->AddRef("title", $title);
$title = "друго заглавие"; // Сега вече и променливата $title в самия темплейт се променя на "друго заглавие"
// А може да си добавим и масив от променливи. Има и втори параметър true/false, който указва дали да се презапишат съществуващи променливи с еднакви имена (true) или да се запазят старите (false).
$t->AddVars( array( 'var1' => 'test', 'var2' => 'test2') );
// Показваме темплейта по един от трите начина
$t->ShowOutput();
?>
</body>
</html>
<?php
/**
* @author Stormbreaker
* @copyright 2010
*/
class Template
{
public $TemplatePath, $UseOB, $OutputCompiled = false, $Output;
public $RelativePath, $AbsolutePath;
private $ReplaceSet = false, $Replace;
public $RegisterVariables = true, $OutputBuffer, $Vars = array();
public function __construct($TemplatePath, $UseOutputBuffering = false, $RegisterVariables = true)
{
if ( $UseOutputBuffering )
$this->StartOutputBuffer();
$this->LoadTemplate($TemplatePath);
$this->RegisterVariables = $RegisterVariables;
}
public static function Load($path, $vars = false, $show = true)
{
$t = new Template($path);
if ( $vars !== false )
$t->AddVars($vars);
$out = $t->GetOutput();
if ( $show )
echo $out;
return $out;
}
public function StartOutputBuffer()
{
$this->UseOB = true;
ob_start();
}
public function LoadTemplate($Template)
{
if ( !is_file($Template) )
die('Could not load template: '. $Template);
$this->TemplatePath = $Template;
$this->AbsolutePath = dirname($Template);
$this->RelativePath = str_replace(str_replace("\\", "/", $_SERVER['DOCUMENT_ROOT']), "", str_replace("\\", "/", $this->AbsolutePath);
}
public function StopOutputBuffer($GetAllLevels = true)
{
$this->OutputBuffer = '';
if ( $GetAllLevels )
while ( ob_get_level() )
$this->OutputBuffer .= ob_get_clean();
else
$this->OutputBuffer = ob_get_clean();
$this->UseOB = false;
return $this->OutputBuffer;
}
public function AddVar($name, $val)
{
$this->Vars[$name] = $val;
}
public function AddRef($name, &$ref)
{
$this->Vars[$name] = &$ref;
}
public function AddVars($array, $shouldOverride = true)
{
if ( $shouldOverride )
$this->Vars = array_merge($this->Vars, $array);
else
$this->Vars = array_merge($array, $this->Vars);
}
public function Redirect($Url, $Time = 5)
{
header("Refresh: ".$Time."; url=". $Url);
if ( !$this->OutputShown )
$this->ShowOutput();
}
private function CompileOutput()
{
if ( $this->OutputCompiled )
return false;
extract($this->Vars, EXTR_REFS);
if ( $this->UseOB )
$this->StopOutputBuffer();
ob_start();
require $this->TemplatePath;
$this->Output = ob_get_clean();
if ( $this->ReplaceSet )
foreach ( $this->Replace as $r )
$this->Output = preg_replace($r['search'], $r['replace'], $this->Output);
$this->OutputCompiled = true;
return true;
}
public function GetOutput()
{
$this->CompileOutput();
return $this->Output;
}
public function ShowOutput()
{
echo $this->GetOutput();
}
public function __toString()
{
return $this->GetOutput();
}
}
?>
test