002.
003.
004.
005.
006.
007.
008.
009.
010.
011.
012.
013.
014.defined('FW') || exit(header('HTTP/1.0 400 Bad Request'));
015.
016.class Template {
017. protected static $obj;
018.
019. public $vars;
020. public $includeFiles;
021. public $includeFile;
022. public $templates;
023. public $template;
024. public $contents;
025. protected $_content;
026. protected $_contents;
027. protected $_path;
028.
029. protected function __construct() {
030. $this->vars = array();
031. require_once ROOT_PATH . "lib/template.func.php";
032. }
033.
034.
035.
036.
037.
038.
039. public static function &init() {
040. if (is_null(self::$obj)) {
041. self::$obj = new Template();
042. }
043. return self::$obj;
044. }
045.
046.
047.
048.
049.
050.
051.
052.
053.
054.
055.
056. public function assign($var, $value) {
057. if (is_array($var)) {
058. foreach ($var as $key => $val) {
059. $this->vars[$key] = $val;
060. }
061. } else {
062. $this->vars[$var] = $value;
063. }
064. }
065.
066.
067.
068.
069.
070.
071.
072.
073.
074. public function fetch($templates) {
075. if (is_array($templates)) {
076. $this->templates = $templates;
077. } else {
078. $this->templates = func_get_args();
079. }
080. extract($this->vars);
081.
082. $this->_contents = '';
083. foreach ($this->templates as $this->template) {
084. ob_end_clean();
085. ob_start();
086. $this->_path = $this->getPath($this->template);
087. require $this->_path;
088. $this->_content = ob_get_contents();
089. ob_end_clean();
090. ob_start();
091. $this->_contents .= $this->_content;
092. $this->contents[$this->template] = $this->_content;
093. }
094. return $this->_contents;
095. }
096.
097. public function getPath($path) {
098. $path = explode(".", $path);
099. $num = count($path);
100. if ($num == 1) {
101. return ROOT_PATH . "template" . DIRECTORY_SEPARATOR . $path[0] . ".html";
102. } elseif ($num > 1) {
103. $templatePath = '';
104. $templatePath = $path[$num - 1];
105. array_pop($path);
106. $templatePath = ROOT_PATH . implode(DIRECTORY_SEPARATOR, $path) . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templatePath . ".html";
107. return $templatePath;
108. } else {
109. return false;
110. }
111. }
112.
113. public function display($templates = array()) {
114. if (!is_array($templates)) {
115. $templates = func_get_args();
116. }
117. if (empty($templates)) {
118. foreach ($this->templates as $this->template) {
119. echo $this->contents[$this->template];
120. }
121. } else {
122. echo $this->fetch($templates);
123. }
124. }
125.}
126.
127.