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

An output-kinda plugin
Goto page 1, 2  Next
 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
WMV
Smarty Rookie


Joined: 26 Dec 2009
Posts: 7

PostPosted: Wed Sep 01, 2010 12:27 pm    Post subject: An output-kinda plugin Reply with quote

There are a couple of plugin-filter-types supported in smarty.
- pre
- post
- output

But when using caching is it possible to have an extra plugin-filter that would run like output-filter but the changes would be included in the cache-file?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 01, 2010 1:36 pm    Post subject: Reply with quote

the affects of pre/post/out should be in the cached files.
Back to top
View user's profile Send private message Visit poster's website
WMV
Smarty Rookie


Joined: 26 Dec 2009
Posts: 7

PostPosted: Wed Sep 01, 2010 2:19 pm    Post subject: Reply with quote

function smarty_outputfilter_test($source, $smarty){
return '<!--// start of: `OUTPUT-FILTER` //-->' . $source . '<!--// end of: `OUTPUT-FILTER` //-->';
}

function smarty_postfilter_test($compiled, $smarty){
return '<!--// start of: `POST-FILTER` //-->' . $compiled . '<!--// end of: `POST-FILTER` //-->';
}

function smarty_prefilter_test($source, $smarty){
return '<!--// start of: `PRE-FILTER` //-->' . $source . '<!--// end of: `PRE-FILTER` //-->';
}

/*************************************/
Browser output is:
<!--// start of: `OUTPUT-FILTER` //-->
<!--// start of: `POST-FILTER` //-->
<!--// start of: `PRE-FILTER` //-->
<div>TEST</div>
<!--// end of: `PRE-FILTER` //-->
<!--// end of: `POST-FILTER` //-->
<!--// end of: `OUTPUT-FILTER` //-->


cache-file contains:
<?php /*%%SmartyHeaderCode:211434c7e5f7f8e5214-44890304%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_smarty_tpl->decodeProperties(array (
'file_dependency' =>
array (
'63ce2810ea2314f5c1f35815eb0f278563fb8080' =>
array (
0 => 'W:\\www\\templates\\index.tpl',
1 => 1283350259,
),
),
'nocache_hash' => '211434c7e5f7f8e5214-44890304',
'has_nocache_code' => false,
'cache_lifetime' => 3600,
)); /*/%%SmartyHeaderCode%%*/?>
<!--// start of: `POST-FILTER` //-->
<!--// start of: `PRE-FILTER` //-->
<div>TEST</div>
<!--// end of: `PRE-FILTER` //-->
<!--// end of: `POST-FILTER` //-->

compiled_template-file contains:
<!--// start of: `POST-FILTER` //-->
<?php /* Smarty version Smarty3-SVN$Rev: 3286 $, created on 2010-09-01 16:13:19
compiled from "W:\www\templates\index.tpl" */ ?>
<?php /*%%SmartyHeaderCode:211434c7e5f7f8e5214-44890304%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_smarty_tpl->decodeProperties(array (
'file_dependency' =>
array (
'63ce2810ea2314f5c1f35815eb0f278563fb8080' =>
array (
0 => 'W:\\www\\templates\\index.tpl',
1 => 1283350259,
),
),
'nocache_hash' => '211434c7e5f7f8e5214-44890304',
'function' =>
array (
),
'has_nocache_code' => false,
)); /*/%%SmartyHeaderCode%%*/?>
<!--// start of: `PRE-FILTER` //-->
<div>TEST</div>
<!--// end of: `PRE-FILTER` //-->
<!--// end of: `POST-FILTER` //-->


/********************/
as far as i can see the outputfilter is run on fetch and not stored inside the cachefiles.
Back to top
View user's profile Send private message
WMV
Smarty Rookie


Joined: 26 Dec 2009
Posts: 7

PostPosted: Wed Sep 01, 2010 2:44 pm    Post subject: fetch in smarty.class.php Reply with quote

i looked up and found this:

// return redered template
if (isset($this->autoload_filters['output']) || isset($this->registered_filters['output'])) {
$_output = Smarty_Internal_Filter_Handler::runFilter('output', $_template->getRenderedTemplate(), $this, $_template);
} else {
$_output = $_template->getRenderedTemplate();
}

In the smarty-fetch function.
So the outputfilter is not saved to the cachefiles
Back to top
View user's profile Send private message
asterix
Smarty Rookie


Joined: 04 Dec 2006
Posts: 8

PostPosted: Mon Nov 15, 2010 8:11 am    Post subject: Reply with quote

I'm using Smarty 3.0.4 and I have the same issue. Outputfilters are not saved in cache.

At first run, the page is displayed correctly, but at second run, the output from the outputfilter is missing because is not saved in the cache.

Is there a solution for problem?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Nov 15, 2010 3:09 pm    Post subject: Reply with quote

Note the result of ouput filters is not saved in the cache, but it runs each time when the it gets fetched. The resion is that the cached template still can contain nocache code which gets rendered when the template is read from cache. but definition the output filter should run over the final output.
Back to top
View user's profile Send private message
asterix
Smarty Rookie


Joined: 04 Dec 2006
Posts: 8

PostPosted: Tue Nov 16, 2010 9:15 am    Post subject: Reply with quote

This means that now is convenient to have trimwhitespaces as postfilter and not as outputfilter. The current behaviour with Smarty2 is to cache the output of trimwhitespaces.

Am i correct?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Nov 16, 2010 1:56 pm    Post subject: Reply with quote

The caveat is that when you use trimwhitespace as postfilter the ouput of variables will not be get trimmed. If this is sufficient it would be anyway the better solution, as the filter will run just once after compilation and not on each rendering.

I just got the idea that the output filter could be cached in cases when the template does not contain nocache code and will run at fetch() otherwise.
I will look into this a bit further.
Back to top
View user's profile Send private message
asterix
Smarty Rookie


Joined: 04 Dec 2006
Posts: 8

PostPosted: Sat Nov 27, 2010 10:30 am    Post subject: Reply with quote

Will be possible to cache the output of outputfilters without the nocache parts?
In Smarty2 this is possible. I have a block function nocache declared as cacheable=false and the outputfilters works in this way.
Right now, I just can't porting my codebase to Smarty3 because of this problem.

Ciao,

Alex
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Nov 27, 2010 3:12 pm    Post subject: Reply with quote

I change the behaviour on output filters to that of Smarty2. The output filter will run before the cache file is written. In consequence the filter does not run over the output of nocache sections.

This change is in the SVN trunk now.
Back to top
View user's profile Send private message
asterix
Smarty Rookie


Joined: 04 Dec 2006
Posts: 8

PostPosted: Sun Nov 28, 2010 1:33 pm    Post subject: Reply with quote

Thanks, now it works correctly.
Back to top
View user's profile Send private message
DerRebell
Smarty Rookie


Joined: 10 Jul 2012
Posts: 15
Location: Dresden/Germany

PostPosted: Tue Sep 04, 2012 12:54 pm    Post subject: Reply with quote

Could it be that this feature has been rejected? The result of my output filter test is not included in the cache file created by Smarty.

Im using Smarty 3.1.11.

Example code:

Code:


function smarty_function_output_collect_block($params, Smarty_Internal_Template $template) {
   $template->smarty->registerFilter('output', 'replace_all_collect_blocks');
   return '%%collect-block-'.$params['name'].'%%';
}

function replace_all_ty_collect_blocks($tpl_source, Smarty_Internal_Template $template) {
   $tpl_source = str_replace('%%collect-block-'.$name.'%%', 'replaced by something', $tpl_source);
   return $tpl_source;
}



In the first run, the '%%collect-block-name%%' has been replaced by 'replaced by something', but not in the second.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Sep 04, 2012 2:47 pm    Post subject: Reply with quote

Quote:
function replace_all_ty_collect_blocks($tpl_source, Smarty_Internal_Template $template) {
$tpl_source = str_replace('%%collect-block-'.$name.'%%', 'replaced by something', $tpl_source);
return $tpl_source;
}

In the function above the PHP variabble $name is not defined. So the strings will not match.

I think you wanted to use the Smarty template varibale $name. The correponding code looks like this
Code:
function replace_all_ty_collect_blocks($tpl_source, Smarty_Internal_Template $template) {
   $tpl_source = str_replace('%%collect-block-'.$template->getTemplateVars('name').'%%', 'replaced by something', $tpl_source);
   return $tpl_source;
Back to top
View user's profile Send private message
DerRebell
Smarty Rookie


Joined: 10 Jul 2012
Posts: 15
Location: Dresden/Germany

PostPosted: Thu Sep 06, 2012 12:54 pm    Post subject: Reply with quote

Hi U.Tews. Im sorry, I've simplified my code and made errors. Typically, the variable $name is defined in this context.

My problem is simple to describe: the result after processing the output filters is not written into the cache file. But I require this feature to realize what I want: To write a collector function:

Code:

<html>
<head>
{collector_block name="head"/}
</head>
<body>
... some HTML code ...
{collector_block name="head"}
<script type="text/javascript" src="..."/>
<style type="text/css" src="..." />
{/collector}
... some other HTML code ...
{collector_block name="head"}
<script type="text/javascript" src="..."/>
<style type="text/css" src="..." />
{/collector}
</body>
</html>


I want to collect code which should be placed at another position in the document (head).
I wrote two smarty plugins:

block.collector_block:
Code:

function smarty_block_collector_block($params, $content, Smarty_Internal_Template $template, &$repeat) {
    $GLOBALS['collector_blocks'][$params['name']][] = $content;
}


function.collector_block
Code:

function smarty_function_collector_block($params, Smarty_Internal_Template $template) {
   $template->smarty->registerFilter('output', 'replace_all_collector_blocks');
   return '%%collector-block-'.$params['name'].'%%';
}

function replace_all_collector_blocks($tpl_source, Smarty_Internal_Template $template) {
   foreach ($GLOBALS['collector_blocks'] as $name => $blocks) {
      $tpl_source = str_replace('%%collector-block-'.$name.'%%', join('',$blocks), $tpl_source);
   }
   return $tpl_source;
}


When I run this code, the output filter replaces all %%collector-blocks%% with its content. But the cache only contains the strings, because it does what it should, skip running the collector block functions or output filters.

If the output filter would write its result into the cache file, everything would be fine.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Sep 09, 2012 11:08 pm    Post subject: Reply with quote

The problem is that your templates do contain also nocache code. In such case Smarty will not run the output filter before the cache file is saved but each time the cache file is read, other the filter would not run over the complete final output.

If the page does not contain nocache code, the output filter runs before the cache file is written.

The only workaround I currently see is that you run the collector plugins also as nocache.

Code:
<html>
<head>
{collector_block name="head" nocache}
</head>
<body>
... some HTML code ...
{collector name="head" nocache}
... some Head code ...<br>
{/collector}
... some other HTML code ...<br>
{collector name="head" nocache}
... some other Head code ...
{/collector}
</body>
</html>
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 -> Smarty 3 All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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