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