Changeset 2334


Ignore:
Timestamp:
May 8, 2008, 3:13:13 AM (16 years ago)
Author:
rvelices
Message:

2 template features:

  • added a {html_head} smarty block - allow any template file to add content just before </head> tag (handy for plugins and allows to move more presentation logic to tpls); the content is usually <style> or <link> which must appear inside html <head> tag
  • by config allow some language strings to be replaced during template compilation -> better performance. drawback: changes in the language file will not be propagated until template is recompiled.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/config_default.inc.php

    r2313 r2334  
    691691// or other plugin variables etc
    692692$conf['local_data_dir'] = dirname(dirname(__FILE__)).'/_data';
     693
     694// if true, some language strings are replaced during template compilation
     695// (insted of template output). this results in better performance. however
     696// any change in the language file will not be propagated until you purge
     697// the compiled templates from the admin / maintenance menu
     698$conf['compiled_template_cache_language'] = false;
     699
    693700?>
  • trunk/include/template.class.php

    r2299 r2334  
    4444  var $files = array();
    4545
     46  // used by html_head smarty block to add content before </head>
     47  var $html_head_elements = array();
     48
    4649  function Template($root = ".", $theme= "")
    4750  {
     
    5053    $this->smarty = new Smarty;
    5154    $this->smarty->debugging = $conf['debug_template'];
    52     //$this->smarty->force_compile = true;
    5355
    5456    if ( isset($conf['compiled_template_dir'] ) )
     
    7779    $this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') );
    7880    $this->smarty->register_modifier( 'explode', array('Template', 'mod_explode') );
     81    $this->smarty->register_block('html_head', array(&$this, 'block_html_head') );
     82    if ( $conf['compiled_template_cache_language'] )
     83    {
     84      $this->smarty->register_prefilter( array(&$this, 'prefilter_language') );
     85    }
    7986
    8087    if ( !empty($theme) )
     
    96103    $real_dir = realpath($dir);
    97104    $compile_id = crc32( $real_dir===false ? $dir : $real_dir);
    98     $this->smarty->compile_id = sprintf('%08X', $compile_id );
     105    $this->smarty->compile_id = base_convert($compile_id, 10, 36 );
    99106  }
    100107
     
    224231    $this->smarty->assign( 'TAG_INPUT_ENABLED',
    225232      ((is_adviser()) ? 'disabled="disabled" onclick="return false;"' : ''));
     233
     234    global $conf, $lang_info;
     235    if ( $conf['compiled_template_cache_language'] and isset($lang_info['code']) )
     236    {
     237      $save_compile_id = $this->smarty->compile_id;
     238      $this->smarty->compile_id .= '.'.$lang_info['code'];
     239    }
     240   
    226241    $v = $this->smarty->fetch($this->files[$handle], null, null, false);
     242   
     243    if (isset ($save_compile_id) )
     244    {
     245      $this->smarty->compile_id = $save_compile_id;
     246    }
     247     
    227248    if ($return)
    228249    {
     
    239260  {
    240261    $this->parse($handle, false);
     262    $this->flush();
     263  }
     264
     265  function flush()
     266  {
     267    if ( count($this->html_head_elements) )
     268    {
     269      $search = "\n</head>";
     270      $pos = strpos( $this->output, $search );
     271      if ($pos !== false)
     272      {
     273        $this->output = substr_replace( $this->output, "\n".implode( "\n", $this->html_head_elements ), $pos, 0 );
     274      } //else maybe error or warning ?
     275      $this->html_head_elements = array();
     276    }
    241277    echo $this->output;
    242278    $this->output='';
    243 
    244   }
    245 
     279  }
    246280
    247281  /** flushes the output */
     
    250284    $start = get_moment();
    251285
    252     echo $this->output;
    253     $this->output='';
     286    $this->flush();
    254287
    255288    if ($this->smarty->debugging)
     
    284317    return explode($delimiter, $text);
    285318  }
     319 
     320  /**
     321   * This smarty "html_head" block allows to add content just before
     322   * </head> element in the output after the head has been parsed. This is
     323   * handy in order to respect strict standards when <style> and <link>
     324   * html elements must appear in the <head> element           
     325   */
     326  function block_html_head($params, $content, &$smarty, &$repeat)
     327  {
     328    $content = trim($content);
     329    if ( !empty($content) )
     330    { // second call
     331      if ( empty($this->output) )
     332      {//page header not parsed yet
     333        $this->append('head_elements', $content);
     334      }
     335      else
     336      {
     337        $this->html_head_elements[] = $content;
     338      }
     339    }
     340  }
     341 
     342  /**
     343   * Smarty prefilter to allow caching (whenever possible) language strings
     344   * from templates.
     345   */
     346  function prefilter_language($source, &$smarty)
     347  {
     348    global $lang;
     349    $ldq = preg_quote($this->smarty->left_delimiter, '~');
     350    $rdq = preg_quote($this->smarty->right_delimiter, '~');
     351   
     352    $regex = "~$ldq *\'([^'$]+)\'\|@translate *$rdq~";
     353    $source = preg_replace( $regex.'e', 'isset($lang[\'$1\']) ? $lang[\'$1\'] : \'$0\'', $source);
     354
     355    $regex = "~$ldq *\'([^'$]+)\'\|@translate\|~";
     356    $source = preg_replace( $regex.'e', 'isset($lang[\'$1\']) ? \'{\'.var_export($lang[\'$1\'],true).\'|\' : \'$0\'', $source);
     357
     358    return $source;
     359  }
    286360}
    287361
  • trunk/template/yoga/admin/plugins_list.tpl

    r2293 r2334  
    1919  </tr>
    2020</thead>
     21{html_head} {*add the style to html head for strict standard compliance*}
     22<style type="text/css">
     23TABLE.table2 TR TD.pluginState {ldelim}
     24  padding-left:16px;
     25}
     26TABLE.table2 TR TD.active {ldelim}
     27  background: url({$ROOT_URL}{$themeconf.admin_icon_dir}/plugin_active.gif) no-repeat center left
     28}
     29TABLE.table2 TR TD.inactive {ldelim}
     30  background: url({$ROOT_URL}{$themeconf.admin_icon_dir}/plugin_inactive.gif) no-repeat center left
     31}
     32</style>
     33{/html_head}
    2134{foreach from=$plugins item=plugin name=plugins_loop}
    2235        <tr class="{if $smarty.foreach.plugins_loop.index is odd}row1{else}row2{/if}">
    23         <td style="padding-left:16px; {if not empty($plugin.STATE)}background: url({$ROOT_URL}{$themeconf.admin_icon_dir}/plugin_{$plugin.STATE}.gif) no-repeat center left{/if}">
     36        <td class="pluginState{if not empty($plugin.STATE)} {$plugin.STATE}{/if}">
    2437                {$plugin.NAME}
    2538        </td>
  • trunk/template/yoga/month_calendar.tpl

    r2265 r2334  
    3737 </tr>
    3838 </thead>
    39 
     39{html_head} {*add the style to html head for strict standard compliance*}
     40<style type="text/css">
     41TABLE.calMonth TBODY TD, TABLE.calMonth TBODY TD DIV.calImg {ldelim}
     42  width:{$chronology_calendar.month_view.CELL_WIDTH}px;height:{$chronology_calendar.month_view.CELL_HEIGHT}px;
     43}
     44</style>
     45{/html_head}
    4046 {foreach from=$chronology_calendar.month_view.weeks item=week}
    4147 <tr>
     
    4551                        <td class="calDayCellFull">
    4652                                <div class="calBackDate">{$day.DAY}</div><div class="calForeDate">{$day.DAY}</div>
    47                                 <div class="calImg" style="width:{$chronology_calendar.month_view.CELL_WIDTH}px;height:{$chronology_calendar.month_view.CELL_HEIGHT}px;">
     53                                <div class="calImg">
    4854                                        <a href="{$day.U_IMG_LINK}">
    4955                                                <img style="{$day.IMAGE_STYLE}" src="{$day.IMAGE}" alt="{$day.IMAGE_ALT}" title="{$pwg->l10n_dec('%d element','%d elements', $day.NB_ELEMENTS)}" />
     
    5157                                </div>
    5258                {else}
    53                         <td class="calDayCellEmpty" style="width:{$chronology_calendar.month_view.CELL_WIDTH}px;height:{$chronology_calendar.month_view.CELL_HEIGHT}px;">{$day.DAY}
     59                        <td class="calDayCellEmpty">{$day.DAY}
    5460                {/if}
    5561        {else}
    56                 <td class="calDayCellBlank" style="width:{$chronology_calendar.month_view.CELL_WIDTH}px;height:{$chronology_calendar.month_view.CELL_HEIGHT}px;">
     62                <td class="calDayCellBlank">
    5763        {/if}
    5864        </td>
Note: See TracChangeset for help on using the changeset viewer.