Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

PB with output filter plugin

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
tonysmarty
Smarty Rookie


Joined: 05 Oct 2006
Posts: 7

PostPosted: Mon Oct 30, 2006 10:16 am    Post subject: PB with output filter plugin Reply with quote

Hello,

I am doing some tests with a little output filter plugin.
I have an error I don't understand :

[ERROR]
Fatal error: Smarty error: [in test_smarty.tpl line 3]: syntax error: unrecognized tag 'rewrite_url' (Smarty_Compiler.class.php, line 583) in f:\www\tests\class\smarty\libs\Smarty.class.php on line 1095
[/ERROR]

My template test_smarty.tpl :

[TEMPLATE]

<html>
{assign var="resultat" value='index.php?cat=21'}
{rewrite_url resultat}
</html>

[/TEMPLATE]

My plugin file is called "outputfilter.rewrite_url.php" and is placed in the plugins directory of my application.

[PLUGIN]

<?php
function smarty_outputfilter_rewrite_url($params, &$Smarty)
{
$pattern = array('/index\.php\?cat=([0-9]+)/');

$replace = array('index-cat-$1.html');
echo "start of rewrite_url"; // for tests
return preg_replace($pattern, $replace, $params['string']);
$smarty->assign('resultat',$params['string']);
echo "end of rewrite_url"; // for tests
}
?>

[/PLUGIN]

The plugin is loaded in my Smarty class [

[CLASS]
<?php
//define('TEST_ROOT_PATH', '/www/iasext/tests');
define('TEST_ROOT_PATH', 'f:/www/tests');
define('SMARTY_DIR', TEST_ROOT_PATH.'/class/smarty/libs/');

require_once SMARTY_DIR.'Smarty.class.php';

// charge la librairie Smarty

class Smarty_test extends Smarty {
function Smarty_test() {

// Constructeur de la classe. Appelé automatiquement
// à l’instanciation de la classe.
$this->Smarty();
$this->template_dir = SMARTY_DIR.'template/templates/';
$this->compile_dir = SMARTY_DIR.'template/templates_c/';
$this->config_dir = SMARTY_DIR.'template/configs/';
$this->cache_dir = SMARTY_DIR.'template/cache/';
$this->compile_check = false;
$this->debugging = false;
$this->caching = false;
$this->load_filter('output', 'rewrite_url');
$this->assign('app_name','test');
}
}
?>
[/CLASS]

I hope my presentation is consize and comprehensible
Someone for an idea please ?
Best regards

Tonysmarty
Back to top
View user's profile Send private message
skyfly
Smarty Rookie


Joined: 07 Nov 2006
Posts: 31
Location: Poland

PostPosted: Tue Nov 07, 2006 11:24 am    Post subject: Reply with quote

Maybe it is also necessary to call register_postfilter() method...
Back to top
View user's profile Send private message Visit poster's website
::downtown::
Smarty Rookie


Joined: 20 May 2005
Posts: 33
Location: Berlin

PostPosted: Fri Nov 10, 2006 10:44 am    Post subject: Reply with quote

I think a modfier is the better way to solve your problem.
Code:

{assign var="resultat" value='index.php?cat=21'}
{$resultat|rewrite_url}



Code:

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     modifier.rewrite_url.php
 * Type:     modifier
 * Name:     rewrite_url
 * -------------------------------------------------------------
 */
function smarty_modifier_rewrite_url($url)
{
    //do your rewrite
    return $url;
}
?>

_________________
Erik Seifert
http://www.b-connect.de
Back to top
View user's profile Send private message Send e-mail Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Fri Nov 10, 2006 3:13 pm    Post subject: Reply with quote

The only thing with using a modifier is you would have to go back and edit all your templates...

A filter does a global search/replace type thing without the need to edit your templates.
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
bet0x
Smarty n00b


Joined: 06 Jan 2007
Posts: 1

PostPosted: Sat Jan 06, 2007 3:45 am    Post subject: Reply with quote

There is a help from me, if you want rewrite urls do this:

function.to_url.php:

Code:

<?php

function smarty_function_to_url($params, &$smarty)
{
   global $mod_rewrite_urls,$links;

   !is_array($params) ? $params=array():"";
   if ($params['re']){
      $remove_empty = true;
      unset($params['re']);
   }

   if ($mod_rewrite_urls){

      foreach ($params as $key=>$value){

               if ($key == "a"){
                 $u[] = $links[$value]?$links[$value]:$value;
                 $x = $value;
               }elseif( ($value  or (!$value and !$remove_empty)) and $key !="query"){
                 $x = $value;
                 $value =="none" ? $value="":"";
                 $u[] = $key.$value;
               }
      }
      if ($u) $url = join ("/",$u).($x !="none" ?".html":"");

   }else{

      foreach ($params as $key=>$value){

               if ($key=="a"){
                 $u[] = "a=". ($links[$value]?$links[$value]:$value);
               }elseif ($key !="query"){
                 $u[] = $key."=". $value;
               }
      }

      if ($u) $ux ="?".join ("&amp;",$u);
      $url = "index.php".$ux;
   }

   if ($mod_rewrite_urls and $params['query']){
        $url .= "?".$params['query'];
   }elseif($params['query']){

        $url .= (strstr($url,"?") ? "&amp;":"?").$params['query'];
   }

     return HOST_PATH.$url;
}

?>


Also an conbert_to_url function:

Code:

function convert_to_url($url,$misc=true,$addvars=""){

global $mod_rewrite_urls,$links;

   if ($mod_rewrite_urls){

      $url_parts =  parse_url($url);
      parse_str($url_parts['query'],$query);

/*      $url = $links[$query['a']]."/";
      unset ($query['a']);
      $url .= join("/",$query);*/

      foreach ($query as $key=>$value){

               if ($key == "a"){
                  $u[] = $links[$value]?$links[$value]:$value;

               }else{
                 $u[] = $key.$value;
               }
      }
      if ($u) $url = join ("/",$u).($misc?"/":"");
   }

   if ($mod_rewrite_urls and $addvars){
        $url .= "?".$addvars;
   }elseif($query and $addvars){
        $url .= (strstr($url,"?") ? "&amp;":"?").$addvars;
   }

    return htmlentities(HOST_PATH.$url);
}


First Example:
<a href="{to_url foo="foo" bar="bar"}">Link Here</a>
Second Example:

Code:

$page_href = convert_to_url("index.php?a=".$links['topic']."&t=".$topic['tid'],false); // ."&page="
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins 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