Changeset 3998


Ignore:
Timestamp:
Oct 9, 2009, 3:00:33 AM (11 years ago)
Author:
patdenice
Message:

merge r3927, r3928, r3951, r3953 from trunk to branch 2.0
r3927: Add Smarty's prefilter capability. see topic:16219 (FR).
r3928: Improve template prefilter functions
r3951: Allow to add prefilters, postfilters and output filters to templates.
r3953: Simplification of commit 3951.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.0/include/template.class.php

    r3208 r3998  
    4747  var $extents = array();
    4848
     49  // Templates prefilter from external sources (plugins)
     50  var $external_filters = array();
     51 
    4952  // used by html_head smarty block to add content before </head>
    5053  var $html_head_elements = array();
     
    291294      ((is_adviser()) ? 'disabled="disabled" onclick="return false;"' : ''));
    292295
     296    $save_compile_id = $this->smarty->compile_id;
     297    $this->load_external_filters($handle);
     298
    293299    global $conf, $lang_info;
    294300    if ( $conf['compiled_template_cache_language'] and isset($lang_info['code']) )
    295301    {
    296       $save_compile_id = $this->smarty->compile_id;
    297302      $this->smarty->compile_id .= '.'.$lang_info['code'];
    298303    }
     
    300305    $v = $this->smarty->fetch($this->files[$handle], null, null, false);
    301306
    302     if (isset ($save_compile_id) )
    303     {
    304       $this->smarty->compile_id = $save_compile_id;
    305     }
     307    $this->smarty->compile_id = $save_compile_id;
     308    $this->unload_external_filters($handle);
    306309
    307310    if ($return)
     
    334337      $this->html_head_elements = array();
    335338    }
     339
    336340    echo $this->output;
    337341    $this->output='';
     
    420424  }
    421425
     426 /**
     427   * This function allows to declare a Smarty prefilter from a plugin, thus allowing
     428   * it to modify template source before compilation and without changing core files
     429   * They will be processed by weight ascending.
     430   * http://www.smarty.net/manual/en/advanced.features.prefilters.php
     431   */
     432  function set_prefilter($handle, $callback, $weight=50)
     433  {
     434    $this->external_filters[$handle][$weight][] = array('prefilter', $callback);
     435    ksort($this->external_filters[$handle]);
     436  }
     437
     438  function set_postfilter($handle, $callback, $weight=50)
     439  {
     440    $this->external_filters[$handle][$weight][] = array('postfilter', $callback);
     441    ksort($this->external_filters[$handle]);
     442  }
     443
     444  function set_outputfilter($handle, $callback, $weight=50)
     445  {
     446    $this->external_filters[$handle][$weight][] = array('outputfilter', $callback);
     447    ksort($this->external_filters[$handle]);
     448  }
     449 
     450 /**
     451   * This function actually triggers the filters on the tpl files.
     452   * Called in the parse method.
     453   * http://www.smarty.net/manual/en/advanced.features.prefilters.php
     454   */
     455  function load_external_filters($handle)
     456  {
     457    if (isset($this->external_filters[$handle]))
     458    {
     459      $compile_id = '';
     460      foreach ($this->external_filters[$handle] as $filters)
     461      {
     462        foreach ($filters as $filter)
     463        {
     464          list($type, $callback) = $filter;
     465          $compile_id .= $type.( is_array($callback) ? implode('', $callback) : $callback );
     466          call_user_func(array($this->smarty, 'register_'.$type), $callback);
     467        }
     468      }
     469      $this->smarty->compile_id .= '.'.base_convert(crc32($compile_id), 10, 36);
     470    }
     471  }
     472
     473  function unload_external_filters($handle)
     474  {
     475    if (isset($this->external_filters[$handle]))
     476    {
     477      foreach ($this->external_filters[$handle] as $filters)
     478      {
     479        foreach ($filters as $filter)
     480        {
     481          list($type, $callback) = $filter;
     482          call_user_func(array($this->smarty, 'unregister_'.$type), $callback);
     483        }
     484      }
     485    }
     486  }
     487
    422488  static function prefilter_white_space($source, &$smarty)
    423489  {
     
    465531  }
    466532}
     533
    467534
    468535/**
Note: See TracChangeset for help on using the changeset viewer.