Skip to content

Instantly share code, notes, and snippets.

@rolife
Created February 6, 2013 01:08
Show Gist options
  • Save rolife/4719320 to your computer and use it in GitHub Desktop.
Save rolife/4719320 to your computer and use it in GitHub Desktop.
Что-то похожее на шаблонизатор.. )) Хотя на самом деле, лучше обращаться напрямую к переменным через шорт теги.
This is test template.
Name var: {name}
Second var: {second}
The end :)
<?php
/**
* Created by RoLife.
* Date: 06.02.13 01:56
* Web-site: http://rolife.ru
*/
class Template {
public $vars = array();
private $error = array();
private $template = '';
function __construct($path) {
$this->template = @file_get_contents($path);
if ($this->template === false) $this->error[] = 'Не удалось загрузить шаблон.';
}
function __set($key,$value) {
$this->vars[$key] = $value;
}
public function Display() {
if (!empty($this->error)) {
return join('<br>',$this->error);
}
foreach ($this->vars as $key => $value) {
$this->template = str_replace('{'.$key.'}',$value,$this->template);
}
return $this->template;
}
}
$tpl = new Template('main.tpl');
$tpl->name = 'Nikolai';
$tpl->second = 'Roller';
echo $tpl->Display();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment