source: trunk/include/template.class.php @ 2231

Last change on this file since 2231 was 2231, checked in by rvelices, 16 years ago
  • index.tpl, menubar.tpl, mainpage_categories.tpl and month_calendar.tpl go smarty
  • better template debugging tweak (the smarty console is shown only once at the end)
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: template.class.php 2231 2008-03-01 13:12:07Z rvelices $
8// | last update   : $Date: 2008-03-01 13:12:07 +0000 (Sat, 01 Mar 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2231 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27
28require 'smarty/libs/Smarty.class.php';
29
30// migrate lang:XXX
31//    sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" my_template.tpl
32// migrate change root level vars {XXX}
33//    sed "s/{pwg_root}/{ROOT_URL}/g" my_template.tpl
34// migrate change root level vars {XXX}
35//    sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g" my_template.tpl
36// migrate all
37//    cat my_template.tpl | sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" | sed "s/{pwg_root}/{ROOT_URL}/g" | sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g"
38
39
40class Template {
41
42  var $smarty;
43
44  var $_old;
45
46  var $output = '';
47
48  // Hash of filenames for each template handle.
49  var $files = array();
50
51  function Template($root = ".", $theme= "")
52  {
53    global $conf;
54
55    $this->smarty = new Smarty;
56    $this->smarty->debugging = $conf['debug_template'];
57    //$this->smarty->force_compile = true;
58
59    if ( isset($conf['compiled_template_dir'] ) )
60    {
61      $compile_dir = $conf['compiled_template_dir'];
62    }
63    else
64    {
65      $compile_dir = $conf['local_data_dir'];
66      if ( !is_dir($compile_dir) )
67      {
68        mkdir( $compile_dir, 0777);
69        file_put_contents($compile_dir.'/index.htm', '');
70      }
71      $compile_dir .= '/templates_c';
72    }
73    if ( !is_dir($compile_dir) )
74    {
75      mkdir( $compile_dir, 0777 );
76      file_put_contents($compile_dir.'/index.htm', '');
77    }
78
79    $this->smarty->compile_dir = $compile_dir;
80
81    $this->smarty->template_dir = $root;
82    $this->smarty->register_function( 'lang', array('Template', 'fn_l10n') );
83
84    $this->smarty->assign_by_ref( 'pwg', new PwgTemplateAdapter() );
85    $this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') );
86
87    include($root.'/theme/'.$theme.'/themeconf.inc.php');
88    $this->smarty->assign('themeconf', $themeconf);
89
90    $this->_old = & new TemplateOld($root, $theme);
91  }
92
93  /** DEPRECATED */
94  function get_themeconf($val)
95  {
96    $tc = $this->smarty->get_template_vars('themeconf');
97    return isset($tc[$val]) ? $tc[$val] : '';
98  }
99
100  /**
101   * Sets the template filename for handle.
102   */
103  function set_filename($handle, $filename)
104  {
105    return $this->set_filenames( array($handle=>$filename) );
106  }
107
108  /**
109   * Sets the template filenames for handles. $filename_array should be a
110   * hash of handle => filename pairs.
111   */
112  function set_filenames($filename_array)
113  {
114    if (!is_array($filename_array))
115    {
116      return false;
117    }
118
119    reset($filename_array);
120    while(list($handle, $filename) = each($filename_array))
121    {
122      if (is_null($filename))
123        unset( $this->files[$handle] );
124      else
125        $this->files[$handle] = $filename;
126    }
127    return true;
128  }
129
130  /**
131   * DEPRECATED - backward compatibility only; use assign
132   */
133  function assign_vars($vararray)
134  {
135    is_array( $vararray ) || die('assign_vars parameter not array');
136    $this->assign( $vararray );
137  }
138
139  /**
140   * DEPRECATED - backward compatibility only; use assign
141   */
142  function assign_var($varname, $varval)
143  {
144    !is_array( $varname ) || die('assign_var parameter name is array');
145    $this->assign( $varname, $varval );
146  }
147
148  /**
149   * Inserts the uncompiled code for $handle as the value of $varname in the
150   * root-level. This can be used to effectively include a template in the
151   * middle of another template.
152   * This is equivalent to assign($varname, $this->parse($handle, true))
153   */
154  function assign_var_from_handle($varname, $handle)
155    {
156      $this->assign($varname, $this->parse($handle, true));
157      return true;
158    }
159
160  /**
161   * DEPRECATED - backward compatibility only
162   */
163  function assign_block_vars($blockname, $vararray)
164  {
165    if (strstr($blockname, '.')!==false)
166    {
167      $blocks = explode('.', $blockname);
168      $blockcount = sizeof($blocks) - 1;
169      $root_var = & $this->smarty->get_template_vars();
170
171      $str = '$root_var';
172      for ($i = 0; $i < $blockcount; $i++)
173      {
174        $str .= '[\'' . $blocks[$i] . '\']';
175        eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:0;');
176        $str .= '[' . $lastiteration . ']';
177      }
178      $str .= '[\'' . $blocks[$blockcount] . '\'][] = $vararray;';
179      eval($str);
180    }
181    else
182      $this->smarty->append( $blockname, $vararray );
183
184    $this->_old->assign_block_vars($blockname, $vararray);
185  }
186
187  /**
188   * DEPRECATED - backward compatibility only
189   */
190  function merge_block_vars($blockname, $vararray)
191  {
192    if (strstr($blockname, '.')!==false)
193    {
194      $blocks = explode('.', $blockname);
195      $blockcount = count($blocks);
196      $root_var = & $this->smarty->get_template_vars();
197
198      $str = '$root_var';
199      for ($i = 0; $i < $blockcount; $i++)
200      {
201        $str .= '[\'' . $blocks[$i] . '\']';
202        eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:-1;');
203        if ($lastiteration==-1)
204        {
205          return false;
206        }
207        $str .= '[' . $lastiteration . ']';
208      }
209      $str = $str.'=array_merge('.$str.', $vararray);';
210      eval($str);
211    }
212    else
213      $this->smarty->append( $blockname, $vararray, true );
214
215    $this->_old->merge_block_vars($blockname, $vararray);
216    return true;
217  }
218
219  /**
220   * Load the file for the handle, eventually compile the file and run the compiled
221   * code. This will add the output to the results or return the result if $return
222   * is true.
223   */
224  function parse($handle, $return=false)
225  {
226    if ( !isset($this->files[$handle]) )
227    {
228      die("Template->parse(): Couldn't load template file for handle $handle");
229    }
230
231    $is_new = true;
232    $params = array('resource_name' => $this->files[$handle], 'quiet'=>true, 'get_source'=>true);
233    if ( $this->smarty->_fetch_resource_info($params) )
234    {
235      if (!preg_match('~{(/(if|section|foreach))|\$[a-zA-Z_]+}~', @$params['source_content']) )
236        $is_new = false;
237    }
238
239    if ($is_new)
240    {
241      $this->smarty->assign( 'ROOT_URL', get_root_url() );
242      $v = $this->smarty->fetch($this->files[$handle], null, null, false);
243    }
244    else
245    {
246      $this->_old->set_filename( $handle, $this->files[$handle] );
247      $v = $this->_old->parse($handle, true);
248    }
249    if ($return)
250    {
251      return $v;
252    }
253    $this->output .= $v;
254  }
255
256  /**
257   * Load the file for the handle, eventually compile the file and run the compiled
258   * code. This will print out the results of executing the template.
259   */
260  function pparse($handle)
261  {
262    $this->parse($handle, false);
263    echo $this->output;
264    $this->output='';
265
266  }
267
268
269  /** flushes the output */
270  function p()
271  {
272    $start = get_moment();
273
274    echo $this->output;
275    $this->output='';
276
277    if ($this->smarty->debugging)
278    {
279      global $t2;
280      $this->smarty->assign(
281        array(
282        'AAAA_DEBUG_OUTPUT_TIME__' => get_elapsed_time($start, get_moment()),
283        'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment())
284        )
285        );
286      require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
287      echo smarty_core_display_debug_console(null, $this->smarty);
288    }
289  }
290
291  /**
292   * Root-level variable concatenation. Appends a  string to an existing
293   * variable assignment with the same name.
294   */
295  function concat_var($tpl_var, $value)
296  {
297    $old_val = & $this->smarty->get_template_vars($tpl_var);
298    if ( isset($old_val) )
299    {
300      $old_val .= $value;
301      $this->_old->concat_var( $tpl_var, $value );
302    }
303    else
304    {
305      $this->assign($tpl_var, $value);
306    }
307  }
308
309  /** see smarty assign http://www.smarty.net/manual/en/api.assign.php */
310  function assign($tpl_var, $value = null)
311  {
312    $this->smarty->assign( $tpl_var, $value );
313
314    if ( is_array($tpl_var) )
315      $this->_old->assign_vars( $tpl_var );
316    else
317      $this->_old->assign_var( $tpl_var, $value );
318  }
319
320  /** see smarty append http://www.smarty.net/manual/en/api.append.php */
321  function append($tpl_var, $value=null, $merge=false)
322  {
323    $this->smarty->append( $tpl_var, $value, $merge );
324  }
325
326  /** see smarty get_template_vars http://www.smarty.net/manual/en/api.get_template_vars.php */
327  function &get_template_vars($name=null)
328  {
329    return $this->smarty->get_template_vars( $name );
330  }
331
332  /** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */
333  function clear_assign($tpl_var)
334  {
335    $this->smarty->clear_assign( $tpl_var );
336  }
337
338  /*static*/ function fn_l10n($params, &$smarty)
339  {
340    return l10n($params['t']);
341  }
342
343  /**
344   * translate variable modifiers - translates a text to the currently loaded
345   * language
346   */
347  /*static*/ function mod_translate($text)
348  {
349    return l10n($text);
350  }
351}
352
353/**
354 * This class contains basic functions that can be called directly from the
355 * templates in the form $pwg->l10n('edit')
356 */
357class PwgTemplateAdapter
358{
359  function l10n($text)
360  {
361    return l10n($text);
362  }
363
364  function l10n_dec($s, $p, $v)
365  {
366    return l10n_dec($s, $p, $v);
367  }
368
369  function sprintf()
370  {
371    $args = func_get_args();
372    return call_user_func_array('sprintf',  $args );
373  }
374}
375
376?>
Note: See TracBrowser for help on using the repository browser.