Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

how to register resources - how to create prefilter

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
wojewsky
Smarty n00b


Joined: 21 Sep 2006
Posts: 4

PostPosted: Tue Nov 03, 2009 2:47 pm    Post subject: how to register resources - how to create prefilter Reply with quote

Hi,

in smarty 2 i've registered some resource but with smarty 3 it does not works. How i've to register new resource in smarty 3?

Also, i've registerd a resource for config files but now config_load ignore it (it works well with smarty 2).
Is it possible to load configs with registerd ressources?

I'm using in smarty 2 a prefilter to load images. In smarty 3 i does not works. The _parse_attrs funktion does not exists any more.
Please, could you write an example prefilter?

Thanks a lot.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Nov 04, 2009 6:16 pm    Post subject: Reply with quote

The register_resource methode does work same way in Smarty2 as in Smarty3 for templates.

Currently it does not work on config files. This will change in the near future.

_parse_attrs is an internal function of the Smarty2 compiler. That does not exist in Smarty3 as it uses completely different methods of parsing.

For what did you use it in an prefilter? Can you please give an example?

Otherwise prefilters work the same in Smarty3
Back to top
View user's profile Send private message
wojewsky
Smarty n00b


Joined: 21 Sep 2006
Posts: 4

PostPosted: Thu Nov 05, 2009 6:58 am    Post subject: Reply with quote

Thank you for your answer. I'll have a look into the register_resource methods.

Here is my old prefilter to include images or an image path:
Code:

<?php
// ----------------------------------------------------------------
/**
 * SMARTY Prefilter zum ermitteln der Pfadangaben der eingebunden Bilder.
 */
// ----------------------------------------------------------------
class Heinze_Smarty_Prefilter_Image {

  // ----------------------------------------------------------------
  /**
   * Konstruktor
   *
   * @param   object $smarty - Refernz des Smarty Objekts
   * @return  void
   */
  // ----------------------------------------------------------------
  public function __construct($smarty) {
    //Prefilter setzen
    $smarty->register_prefilter(array('Heinze_Smarty_Prefilter_Image', 'getImage'));
  }

  // ----------------------------------------------------------------
  // oeffentliche Methoden
  // ----------------------------------------------------------------

  // ----------------------------------------------------------------
  /**
   * Prefilter Funktion zum ermitteln der Bilder
   *
   * @param   string $source - Template-Quelltext
   * @param   object $smarty - Referenz auf das Smarty Objekt
   * @return  string - Template-Quelltext
   */
  // ----------------------------------------------------------------
  public static function getImage($source, &$smarty) {
    //Fuer die Callbackfunktion benoetigen temporaer wir eine globale Smarty-Variable
    if (!Zend_Registry::isRegistered('smartyGetImage')) {
      Zend_Registry::set('smartyGetImage', $smarty);
    }
    //Der auszufuehrende Befehl wird ermittelt
    $source = preg_replace_callback('!'.preg_quote($smarty->left_delimiter, '!').
      'image(.*)'.preg_quote($smarty->right_delimiter, '!').'!Us',
      array('Heinze_Smarty_Prefilter_Image', 'buildImage'), $source);
    //Template-Quelltext zurueckgeben
    return $source;
  }

  // ----------------------------------------------------------------
  /**
   * Funktion zum erstellen der IMG-Tags
   *
   * @param   string $match - Ueberseinstimmender Eintrag
   * @return  string - Smarty Template Source fuer das Suchmuster
   */
  // ----------------------------------------------------------------
  private static function buildImage($match) {
    //Variablen inititalisieren
    $source = '';
    $smarty = Zend_Registry::get('smartyGetImage');
    //Attribute des Befehls ermitteln
    if (!empty($smarty)) {
      $attribut = $smarty->_parse_attrs($match[1]);
    }
    //Pflichtangaben pruefen
    //Die Datei muss uebergeben werden
    if (!isset($attribut['file'])) {
      try {
        throw new Zend_Exception('"file" Parameter fehlt im Smarty-Befehl!');
      } catch (Zend_Exception $e) {
        Zend_Registry::get('exception')->logError($e->getMessage(), $e);
      }
      return false;
    }
    //Der Typ der Ausgabe muss uebergeben werden
    if (!isset($attribut['type'])) {
      try {
        throw new Zend_Exception('"type" Parameter fehlt im Smarty-Befehl!');
      } catch (Zend_Exception $e) {
        Zend_Registry::get('exception')->logError($e->getMessage(), $e);
      }
      return false;
    } else {
      $type = $smarty->_dequote($attribut['type']);
      //und es muss entweder der Pfad oder das ganze Image-Tag ausgegben werden
      if (($type != 'path') && ($type != 'img')) {
        try {
          throw new Zend_Exception('"type" Parameter hat fehlerhafte Werte im Smarty-Befehl!');
        } catch (Zend_Exception $e) {
          Zend_Registry::get('exception')->logError($e->getMessage(), $e);
        }
        return false;
      }
    }
    //ggf. wird eine kustnr fuer Herstellerbilder uebergeben
    $kustnr = (isset($attribut['kustnr'])) ? $smarty->_dequote($attribut['kustnr']) : 0;
    $resourceName = $smarty->_dequote($attribut['file']);

    //Anhand der kustnr wird entschieden wo das Bild gesucht werden soll
    if ($kustnr == 0) {
      $resourceName = Heinze_PathFinder::getPath($resourceName, 'artwork');
    } else {
      $resourceName = Heinze_PathFinder::getPath($resourceName, 'producerImage', sprintf('%05d', $kustnr));
    }
    //wenn das Bild nicht gefunden werden konnte gibt es einen Fehler
    if (empty($resourceName)) {
      try {
        throw new Zend_Exception('Das Bild "'. $smarty->_dequote($attribut['file']) .'" konnte nicht gefunden werden');
      } catch (Zend_Exception $e) {
        Zend_Registry::get('exception')->logError($e->getMessage(), $e);
      }
      return false;
    }
    //Benutzerdefinierte Variablen verarbeiten (alle weiteren Attribute des Befehls)
    $imageParams=array();
    if (!empty($attribut)) {
      foreach ($attribut as $key=>$value) {
        //nur die zusaetzlichen Attribute sollen als Parameter angehaengt werden
        if (($key != 'type') && ($key != 'file')) {
          $imageParams[strtolower($key)] = $smarty->_dequote($value);
        }
      }
    }
    if ($type == 'path') {
      $source .= $resourceName;
    } elseif ($type == 'img') {
      $source .= call_user_func_array(array('Heinze_Image', 'getImageTag'), array(
        $resourceName,
        $imageParams,
      ));
    }
    //Template zurueckgeben
    return $source;
  }
}
?>
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP