/[cvs]/nfo/php/libs/org.netfrag.flib/Site/Template.php
ViewVC logotype

Diff of /nfo/php/libs/org.netfrag.flib/Site/Template.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Thu Dec 19 06:20:39 2002 UTC revision 1.2 by joko, Thu Dec 19 10:33:49 2002 UTC
# Line 3  Line 3 
3  //    $Id$  //    $Id$
4  //    -------------------------------------------------------------------------  //    -------------------------------------------------------------------------
5  //    $Log$  //    $Log$
6    //    Revision 1.2  2002/12/19 10:33:49  joko
7    //    + debugging and bugfixing
8    //
9  //    Revision 1.1  2002/12/19 06:20:39  joko  //    Revision 1.1  2002/12/19 06:20:39  joko
10  //    + initial check-in  //    + initial check-in
11  //  //
12  //    -------------------------------------------------------------------------  //    -------------------------------------------------------------------------
13    
14  class Site_Template {  class Site_Template {
15        
16    var $meta;    var $meta;
17        
18    function start() {    function start() {
19  //print "START<br>";  //print "START<br>";
20    }    }
21    
22    function _register_langtext_safe() {    function _register_langtext_safe() {
23      if (!$this->meta[registered][lt]) {      if (!$this->meta[registered][lt]) {
24        $this->meta[registered][lt] = 1;        $this->meta[registered][lt] = 1;
25        $this->_register_langtext();        $this->_register_langtext();
26      }      }
27    }    }
28    
29    function _register_langtext() {    function _register_langtext() {
30  //print "REGISTER<br>";  //print "REGISTER<br>";
31      // register 'lt' (LangText) template provider      // register 'lt' (LangText) template provider
32      $this->site->smarty->register_resource(      $this->site->smarty->register_resource(
33        "lt",        "lt",
34        array(        array(
35          "db_get_template",          "db_get_template",
36          "db_get_timestamp"          "db_get_timestamp"
37        )        )
38      );      );
39  /*  /*
40        array(        array(
41          "db_get_template",          "db_get_template",
42          "db_get_timestamp",          "db_get_timestamp",
43          "db_get_secure",          "db_get_secure",
44          "db_get_trusted"          "db_get_trusted"
45        )        )
46  */  */
47    }    }
48    
49    function display($identifier, $options) {    function display($identifier, $options) {
50  //print "DISPLAY<br>";  //print "DISPLAY<br>";
51      $this->_build($identifier, $options);      $this->_build($identifier, $options);
52    }    }
53    
54    function get($identifier, $interpolate = array()) {    function get($identifier, $interpolate = array()) {
55  //print "GET<br>";  //print "GET<br>";
56      // TODO: review... what about the cache-key here? use volatile by default or not?      // TODO: review... what about the cache-key here? use volatile by default or not?
57      // for now: per default to be _very_ safe....    ;)      // for now: per default to be _very_ safe....    ;)
58      $options = array(      $options = array(
59        'interpolate' => $interpolate,        'interpolate' => $interpolate,
60        'return' => 1,        'return' => 1,
61        'cache_key' => 'volatile'        'cache_key' => 'volatile'
62      );      );
63  //      'cache_key' => 'volatile'  //      'cache_key' => 'volatile'
64      return $this->_build($identifier, $options);      return $this->_build($identifier, $options);
65    }    }
66    
67    function _build($identifier, $options) {    function _build($identifier, $options) {
68    
69  //print "BUILD identifier $identifier<br>";  //print "BUILD identifier $identifier<br>";
70  //print "options:<br>" . Dumper($options) . "<br>";  //print "options:<br>" . Dumper($options) . "<br>";
71    
72      $this->_register_langtext_safe();      $this->_register_langtext_safe();
73    
74          // TODO: just do '$this->site->smarty->assign($arguments[vars]);' ???          // TODO: just do '$this->site->smarty->assign($arguments[vars]);' ???
75          /*          /*
76          foreach ($arguments[vars] as $name => $value) {          foreach ($arguments[vars] as $name => $value) {
77            $this->site->smarty->assign($name, $value);            $this->site->smarty->assign($name, $value);
78          }          }
79          */          */
80                    
81          // template variable interpolation          // template variable interpolation
82          if ($options['interpolate']) {          if ($options['interpolate']) {
83            //print Dumper($options['interpolate']);            $this->site->smarty->assign($options['interpolate']);
84            $this->site->smarty->assign($options['interpolate']);          }
85            /*          
           foreach ($options['interpolate'] as $key => $val) {  
             print "key: $key - val $val<br>";  
             $this->site->smarty->assign($key, $val);  
           }  
           */  
         }  
           
86  //print "cached: " . $this->site->smarty->is_cached($identifier) . "<br>";  //print "cached: " . $this->site->smarty->is_cached($identifier) . "<br>";
87                    
88          // TODO: refactor this!          // TODO: refactor this!
89          // $smarty_method = 'fetch|display'          // $smarty_method = 'fetch|display'
90          // $cache_key???          // $cache_key???
91          if ($options['return']) {          if ($options['return']) {
92    
93  //print "RETURN<br>";  //print "RETURN<br>";
94    
95            // return template            // return template
96            // use of cache-key per template            // use of cache-key per template
97            if ($cache_key = $options['cache_key']) {            if ($cache_key = $options['cache_key']) {
98  //print "1: $cache_key<br>";  //print "1: $cache_key<br>";
99              if ($cache_key == 'volatile') {              if ($cache_key == 'volatile') {
100                //$this->site->smarty->clear_cache($identifier, $cache_key);                //$this->site->smarty->clear_cache($identifier, $cache_key);
101                //$this->site->smarty->clear_cache('referer.php');                //$this->site->smarty->clear_cache('referer.php');
102              }              }
103              $result = $this->site->smarty->fetch($identifier, $cache_key);              $result = $this->site->smarty->fetch($identifier, $cache_key);
104            } else {            } else {
105  //print "2<br>";  //print "2<br>";
106              $result = $this->site->smarty->fetch($identifier);              $result = $this->site->smarty->fetch($identifier);
107            }            }
108  //print "result: $result<br>";  //print "result: $result<br>";
109    
110          } else {          } else {
111                    
112            // display template            // display template
113            // use of cache-key per template            // use of cache-key per template
114            if ($cache_key = $options['cache_key']) {            if ($cache_key = $options['cache_key']) {
115              $result = $this->site->smarty->display($identifier, $cache_key);              $result = $this->site->smarty->display($identifier, $cache_key);
116            } else {            } else {
117              $result = $this->site->smarty->display($identifier);              $result = $this->site->smarty->display($identifier);
118            }            }
119    
120          }          }
121                                
122          return $result;          return $result;
123    
124    }    }
125    
126  }  }
127    
128    
129  function db_get_template($tpl_name, &$tpl_source, &$smarty_obj)  function db_get_template($tpl_name, &$tpl_source, &$smarty_obj)
130  {  {
131    //print "db_get_template: '$tpl_name', '$tpl_source', '$smarty_obj'<br>";    //print "db_get_template: '$tpl_name', '$tpl_source', '$smarty_obj'<br>";
132    //return $this->site->lt->getlt($tpl_name);    //return $this->site->lt->getlt($tpl_name);
133    global $site;    global $site;
134    if ($template = $site->lt->getlt($tpl_name)) {    if ($template = $site->lt->getlt($tpl_name, array(), 1)) {
135  //print "template: '$template'<br>";  //print "template: '$template'<br>";
136      $tpl_source = $template;      $tpl_source = $template;
137      return true;      return true;
138    }    }
139  }  }
140    
141  function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)  function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
142  {  {
143    $tpl_timestamp = time();    $tpl_timestamp = time();
144    return true;    return true;
145  }  }
146    
147    
148  /*  /*
149    
150  // ----------------------------------------------------------------------------------  // ----------------------------------------------------------------------------------
151  // from: http://smarty.php.net/manual/en/templates.from.elsewhere.html  // from: http://smarty.php.net/manual/en/templates.from.elsewhere.html
152  // ----------------------------------------------------------------------------------  // ----------------------------------------------------------------------------------
153    
154    
155  // from PHP script  // from PHP script
156    
157  // put these function somewhere in your application  // put these function somewhere in your application
158  function db_get_template ($tpl_name, &tpl_source, &$smarty_obj)  function db_get_template ($tpl_name, &tpl_source, &$smarty_obj)
159  {  {
160      // do database call here to fetch your template,      // do database call here to fetch your template,
161      // populating $tpl_source      // populating $tpl_source
162      $sql = new SQL;      $sql = new SQL;
163      $sql->query("select tpl_source      $sql->query("select tpl_source
164                     from my_table                     from my_table
165                    where tpl_name='$tpl_name'");                    where tpl_name='$tpl_name'");
166      if ($sql->num_rows) {      if ($sql->num_rows) {
167          $tpl_source = $sql->record['tpl_source'];          $tpl_source = $sql->record['tpl_source'];
168          return true;          return true;
169      } else {      } else {
170          return false;          return false;
171      }      }
172  }  }
173    
174  function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)  function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
175  {  {
176      // do database call here to populate $tpl_timestamp.      // do database call here to populate $tpl_timestamp.
177      $sql = new SQL;      $sql = new SQL;
178      $sql->query("select tpl_timestamp      $sql->query("select tpl_timestamp
179                     from my_table                     from my_table
180                    where tpl_name='$tpl_name'");                    where tpl_name='$tpl_name'");
181      if ($sql->num_rows) {      if ($sql->num_rows) {
182          $tpl_timestamp = $sql->record['tpl_timestamp'];          $tpl_timestamp = $sql->record['tpl_timestamp'];
183          return true;          return true;
184      } else {      } else {
185          return false;          return false;
186      }      }
187  }  }
188    
189  function db_get_secure($tpl_name, &$smarty_obj)  function db_get_secure($tpl_name, &$smarty_obj)
190  {  {
191      // assume all templates are secure      // assume all templates are secure
192      return true;      return true;
193  }  }
194    
195  function db_get_trusted($tpl_name, &$smarty_obj)  function db_get_trusted($tpl_name, &$smarty_obj)
196  {  {
197      // not used for templates      // not used for templates
198  }  }
199    
200  // register the resource name "db"  // register the resource name "db"
201  $smarty->register_resource("db", array("db_get_template",  $smarty->register_resource("db", array("db_get_template",
202                                         "db_get_timestamp",                                         "db_get_timestamp",
203                                         "db_get_secure",                                         "db_get_secure",
204                                         "db_get_trusted"));                                         "db_get_trusted"));
205    
206  // using resource from php script  // using resource from php script
207  $smarty->display("db:index.tpl");  $smarty->display("db:index.tpl");
208    
209  {* using resource from within Smarty template *}  {* using resource from within Smarty template *}
210  {include file="db:/extras/navigation.tpl"}  {include file="db:/extras/navigation.tpl"}
211    
212  // ----------------------------------------------------------------------------------  // ----------------------------------------------------------------------------------
213    
214  */  */
215    
216    
217  ?>  ?>

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed