Changeset 3951


Ignore:
Timestamp:
Oct 1, 2009, 12:46:48 PM (15 years ago)
Author:
patdenice
Message:

Allow to add prefilters, postfilters and output filters to templates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/template.class.php

    r3928 r3951  
    430430   * http://www.smarty.net/manual/en/advanced.features.prefilters.php
    431431   */
    432   function set_external_filter($handle, $callback, $weight=50)
    433   {
    434     if (isset($this->external_filters[$handle][$weight]))
    435     {
    436       foreach($this->external_filters[$handle][$weight] as $func)
    437       {
    438         if ($func == $callback)
    439         {
    440           return false;
    441         }
    442       }
    443     }
    444     $this->external_filters[$handle][$weight][] = $callback;
     432  function set_prefilter($handle, $callback, $weight=50)
     433  {
     434    $this->external_filters[$handle][$weight][] = array('pre', $callback);
    445435    ksort($this->external_filters[$handle]);
    446     return true;
     436  }
     437
     438  function set_postfilter($handle, $callback, $weight=50)
     439  {
     440    $this->external_filters[$handle][$weight][] = array('post', $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('output', $callback);
     447    ksort($this->external_filters[$handle]);
    447448  }
    448449 
     
    456457    if (isset($this->external_filters[$handle]))
    457458    {
    458       $compile_id = $this->smarty->compile_id;
    459       foreach ($this->external_filters[$handle] as $callbacks)
    460       {
    461         foreach ($callbacks as $callback)
     459      $compile_id = '';
     460      foreach ($this->external_filters[$handle] as $filters)
     461      {
     462        foreach ($filters as $filter)
    462463        {
    463           $compile_id .= $callback;
    464           $this->smarty->register_prefilter($callback);
     464          list($type, $callback) = $filter;
     465          $compile_id .= $type.( is_array($callback) ? implode('', $callback) : $callback );
     466          switch ($type)
     467          {
     468            case 'pre':
     469              $this->smarty->register_prefilter($callback);
     470              break;
     471            case 'post':
     472              $this->smarty->register_postfilter($callback);
     473              break;
     474            case 'output':
     475              $this->smarty->register_outputfilter($callback);
     476              break;
     477          }
    465478        }
    466479      }
    467       $this->smarty->compile_id = base_convert(crc32($compile_id), 10, 36);
     480      $this->smarty->compile_id .= '.'.base_convert(crc32($compile_id), 10, 36);
    468481    }
    469482  }
     
    473486    if (isset($this->external_filters[$handle]))
    474487    {
    475       foreach ($this->external_filters[$handle] as $callbacks)
    476       {
    477         foreach ($callbacks as $callback)
     488      foreach ($this->external_filters[$handle] as $filters)
     489      {
     490        foreach ($filters as $filter)
    478491        {
    479           $this->smarty->unregister_prefilter($callback);
     492          list($type, $callback) = $filter;
     493          switch ($type)
     494          {
     495            case 'pre':
     496              $this->smarty->unregister_prefilter($callback);
     497              break;
     498            case 'post':
     499              $this->smarty->unregister_postfilter($callback);
     500              break;
     501            case 'output':
     502              $this->smarty->unregister_outputfilter($callback);
     503              break;
     504          }
    480505        }
    481506      }
Note: See TracChangeset for help on using the changeset viewer.