|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
tonysmarty Smarty Rookie
Joined: 05 Oct 2006 Posts: 7
|
Posted: Mon Oct 30, 2006 10:16 am Post subject: PB with output filter plugin |
|
|
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 |
|
skyfly Smarty Rookie
Joined: 07 Nov 2006 Posts: 31 Location: Poland
|
Posted: Tue Nov 07, 2006 11:24 am Post subject: |
|
|
Maybe it is also necessary to call register_postfilter() method... |
|
Back to top |
|
::downtown:: Smarty Rookie
Joined: 20 May 2005 Posts: 33 Location: Berlin
|
Posted: Fri Nov 10, 2006 10:44 am Post subject: |
|
|
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 |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
bet0x Smarty n00b
Joined: 06 Jan 2007 Posts: 1
|
Posted: Sat Jan 06, 2007 3:45 am Post subject: |
|
|
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 ("&",$u);
$url = "index.php".$ux;
}
if ($mod_rewrite_urls and $params['query']){
$url .= "?".$params['query'];
}elseif($params['query']){
$url .= (strstr($url,"?") ? "&":"?").$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,"?") ? "&":"?").$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 |
|
|
|
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
|