Skip to content

Instantly share code, notes, and snippets.

@edtoken
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save edtoken/32bf096b80e3bf9025e8 to your computer and use it in GitHub Desktop.

Select an option

Save edtoken/32bf096b80e3bf9025e8 to your computer and use it in GitHub Desktop.
<?php
class MegaOffice {
public $page;
protected $dirParts = './parts/';
protected $dirPages = './pages/';
function __construct(){
$this->page = ($_GET['page'])? $_GET['page'] : 'index';
}
protected function replaceParts($content){
$parts = array();
preg_match_all("/\{\{(.+?)\}\}/is", $content, $layoutParts);
if(count($layoutParts)){
$parts = $layoutParts[1];
}
if(count($parts)){
foreach ($parts as $num => $part) {
$partHTML = $this->getTemplateFile($this->dirParts, $part);
if($partHTML){
$partHTML = $this->replaceParts($partHTML);
}
if(!$partHTML && $partHTML !== ''){
$partHTML = '<h1 style="color:red;position:relative;display:block;">шаблон ' . $part . ' не найден </h1>';
}
$content = str_replace($layoutParts[0][$num], $partHTML, $content);
}
}
return $content;
}
protected function getTemplateFile($dir, $name){
$file = $dir . $name . '.htm';
return @file_get_contents($file);
}
protected function getPageTemplate($name){
return $this->getTemplateFile($this->dirPages, $name);
}
public function render(){
$content = $this->getPageTemplate($this->page);
if(!$content){
return 'Страница не найдена <a href="/new/html/">go to index </a>';
}
return $this->replaceParts($content);
}
}
$Office = new MegaOffice();
echo $Office->render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment