OSDN Git Service

24d2e930df786539656593b4e18de79c94ff4223
[nucleus-jp/nucleus-plugins.git] / trunk / NP_Moblog / sharedlibs / cles / Template.php
1 <?php
2 // vim: tabstop=2:shiftwidth=2
3
4 /**
5   * Template.php ($Revision: 1.1 $)
6   * 
7   * by hsur ( http://blog.cles.jp/np_cles )
8   * $Id: Template.php,v 1.1 2008-05-04 07:04:50 hsur Exp $
9 */
10
11 /*
12   * Copyright (C) 2006 CLES. All rights reserved.
13   *
14   * This program is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU General Public License
16   * as published by the Free Software Foundation; either version 2
17   * of the License, or (at your option) any later version.
18   * 
19   * This program is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   * GNU General Public License for more details.
23   * 
24   * You should have received a copy of the GNU General Public License
25   * along with this program; if not, write to the Free Software
26   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
27   * 
28   * In addition, as a special exception, cles( http://blog.cles.jp/np_cles ) gives
29   * permission to link the code of this program with those files in the PEAR
30   * library that are licensed under the PHP License (or with modified versions
31   * of those files that use the same license as those files), and distribute
32   * linked combinations including the two. You must obey the GNU General Public
33   * License in all respects for all of the code used other than those files in
34   * the PEAR library that are licensed under the PHP License. If you modify
35   * this file, you may extend this exception to your version of the file,
36   * but you are not obligated to do so. If you do not wish to do so, delete
37   * this exception statement from your version.
38 */
39
40 class cles_Template {
41         var $defaultLang = 'japanese-utf8';
42         var $defalutPattern = '#{{(.*?)(\|)?}}#ie';
43         var $lang;
44         var $templateDir;
45
46         function cles_Template($templateDir) {
47                 global $CONF;
48                 $this->templateDir = $templateDir;
49                 $this->lang = ereg_replace( '[\\|/]', '', getLanguageName());
50         }
51
52         function fetch($name, $dir = null, $suffix = 'html') {
53                 $path = $this->templateDir.'/'.( $dir ? strtolower($dir) . '/' : '' ).strtolower($name).'_'.$this->lang.( $suffix ? '.'.strtolower($suffix) : '' );
54                 if ( ! file_exists($path) ){
55                         $path = $this->templateDir.'/'.( $dir ? strtolower($dir) . '/' : '' ).strtolower($name).'_'.$this->defaultLang.( $suffix ? '.'.strtolower($suffix) : '' );
56                         if ( ! file_exists($path) )
57                                 return '';
58                 }
59                 
60                 $fsize = filesize($path);
61                 if ($fsize <= 0) return '';
62                 
63                 $fd = fopen($path, 'r');
64                 $contents = fread($fd, $fsize);
65                 fclose($fd);
66                 return $contents;
67         }
68         
69         function fill($template, $values, $default = null) {
70                 if( $default )
71                         return preg_replace($this->defalutPattern, 'isset($values["$1"]) ? ("$2" ? htmlspecialchars($values["$1"], ENT_QUOTES) : $values["$1"]) : $default', $template);
72                 if( $default === null )
73                         return preg_replace($this->defalutPattern, '("$2") ? htmlspecialchars($values["$1"], ENT_QUOTES) : $values["$1"]', $template);
74                 return preg_replace($this->defalutPattern, 'isset($values["$1"]) ? ("$2" ? htmlspecialchars($values["$1"], ENT_QUOTES) : $values["$1"]) : "{{$1}}" ', $template);
75         }
76 }