php 2007-2009 Barttos Vartolomei Nicolae All rights reserved http bart

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/*
* (c) 2007-2009 Barttos (Vartolomei Nicolae) - All rights reserved.
* http://barttos.net/
* me@barttos.net
* nvartolomei@gmail.com
*/
class bts_template {
public $dir = "";
public $template = null;
private $copy_template = null;
private $data = array ("name" => null);
public $block_data = array ();
public function __construct() {
}
public function load_template($template_name) {
if ($template_name == "" || !file_exists($this->dir . $template_name)) {
die("Can not load template: " . $template_name);
return False;
}
$this->template = file_get_contents($this->dir . $template_name);
if (strpos($this->template, "{include file=") !== False) {
$this->template = preg_replace("#\\{include file=['\"](.+?)['\"]\\}#ies", "\$this->sub_load_template('\\1')", $this->template);
}
$this->copy_template = $this->template;
return True;
}
function sub_load_template($template_name) {
if ($template_name == "" || !file_exists($this->dir . $template_name)) {
die("Can not load template: " . $template_name);
return False;
}
$template = file_get_contents($this->dir . $template_name);
return $template;
}
public function compile($template_name) {
foreach ($this->data as $key_find => $key_replace) {
$find[] = $key_find;
$replace[] = $key_replace;
}
$result = str_replace($find, $replace, $this->copy_template);
if (count($this->block_data)) {
foreach ($this->block_data as $key_find => $key_replace) {
$find_preg[] = $key_find;
$replace_preg[] = $key_replace;
}
$result = preg_replace($find_preg, $replace_preg, $result);
}
if (isset($this->result[$template_name])) $this->result[$template_name] .= $result;
else $this->result[$template_name] = $result;
$this->tpl_clear();
}
private function tpl_clear() {
$this->data = array ("name", null);
$this->block_data = array ();
$this->copy_template = $this->template;
}
public function set($name, $var) {
if( is_array($var) && count($var)) {
foreach ($var as $key => $key_var) {
$this->set($key, $key_var);
}
} else
$this->data[$name] = $var;
}
public function set_block($name, $var) {
if(is_array($var) && count($var) ) {
foreach ($var as $key => $key_var) {
$this->set_block($key, $key_var);
}
} else
$this->block_data[$name] = $var;
}
}