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

{cache} {/cache}
Goto page Previous  1, 2, 3, 4  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 -> Feature Requests
View previous topic :: View next topic  
Author Message
micha80
Smarty Rookie


Joined: 31 Aug 2005
Posts: 7
Location: Berlin

PostPosted: Wed Sep 21, 2005 1:32 pm    Post subject: Reply with quote

no, i'am not kidding. Wink but when i echo the variable $file_name, i get only the name of the file but not the path where it has to be stored.

in my example the echo tag gives to me the following:

Code:
basic.tpl#test


not more. only the filename but no path. i think it has to be written into compiled/cached folder. or am i wrong?

micha
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Sep 22, 2005 7:11 am    Post subject: new version 0.1.4 -- now writes to compile dir Reply with quote

Correct. It actually wants to write to the same location as the calling script which would have been expected to be both a writable location and a template dir. Heh, I really only ever tested this from the command line Smile

It is actually a bit of a PITA to write to a specific directory as it is difficult to pass new setup information to the compiler due to the way the main smarty object interacts with the compiler object.

Never-the-less, I have added a slight hack and have correspondingly updated the code to v0.1.4 (back in the previous post). It should work a lot better now (but not perfect--see below). Basically, it now writes template fragments to a required subdirectory (which must be named 'clipcache') in your compiled directory. You must ensure the directory exists and is writable. Use the extended class method register_clipcache() to make sure that the compile/clipcache directory is also registered as a template directory or do so manually. BTW, if you want to try this, only the prefilter and the extended class need updating.

Unfortunately, this is likely to break for templates in subdirectories (eg: foo/bar.tpl) but I am a bit short on time at the moment so this is still beta. It should be fairly easy to fix that so I'll try to post another update soon. Even then it will probably remain in beta status since there are a few other outstanding issues.
Back to top
View user's profile Send private message
micha80
Smarty Rookie


Joined: 31 Aug 2005
Posts: 7
Location: Berlin

PostPosted: Thu Sep 22, 2005 1:30 pm    Post subject: Reply with quote

thanks a lot,

i'll try this new version and maybe it can fix my new problem Smile. everything works fine but now i got a bad issue with the "is_cached" feature (a logical mistake of myself is also possible). to catch some different php/mysql function, i try to use the "is_cached" feature but i can't get the right file for comparing.

PHP-File:
Code:
$smarty->is_cached('test.tpl', "foo");


TPL-File
Code:
{clipcache id=test group=foo ttl=60}{$TESTVALUE}{/clipcache}


do i something wrong?

micha
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Sep 22, 2005 6:18 pm    Post subject: Reply with quote

I never tested this functionality but in theory it should work; however, you have to mention the id of the clipcache:

[php:1:b6ef9acb01]$smarty->is_cached('test.tpl#test', "foo");[/php:1:b6ef9acb01]
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Sep 22, 2005 8:20 pm    Post subject: Post subject: new version 0.1.5 -- now with better directory Reply with quote

Another little update. I've abandoned file_put_contents and now use Smarty's internal file writing function which has the benefit of working with PHP4 (naturally) and also supports safe directory creation. Now you needn't explicitly create the clipcache directory in your compile directory -- it will be done for you. This implicitly assumes that use_sub_dir = true. This also means that template subdirectories (foo/bar.tpl) should work now.

Still beta -- there are some outstanding issues:
    the following smarty features will probably cause problems as there is no explicit support for them and they have not been tested:
    - resources
    - compile_id
    the {clipcache} close tag is still wonky. It needs a better regex to both use a simpler (and typical) {/clipcache} closing tag and support nesting. I haven't needed it yet (in fact, I haven't needed any of this Smile).
    nesting isn't completely tested, so please report issues.

There is also an additional feature I am considering which I would personally find useful: allowing for alternate tags to be defined. The idea is:

Code:
{clipcache id=foo group=bar ttl=300 ldelim=(: rdelim=:)}

{foobar} <-- not interpreted as a smarty tag in this block
(: include file=foobar.txt :) <-- interpreted as a smarty tag in this block

{/clipcache  id=foo group=bar ttl=300 ldelim=(: rdelim=:)}
Back to top
View user's profile Send private message
micha80
Smarty Rookie


Joined: 31 Aug 2005
Posts: 7
Location: Berlin

PostPosted: Fri Sep 23, 2005 10:58 am    Post subject: Reply with quote

hi,

i've found the right condition to check with the is_cached function. you have to send the path (where the file is stored) and the filename to the function:

Code:
is_cached('/home/eigen/compile/clipcache/test.tpl#test', 'foo')


i hope this will help somebody.

micha
Back to top
View user's profile Send private message
micha80
Smarty Rookie


Joined: 31 Aug 2005
Posts: 7
Location: Berlin

PostPosted: Fri Sep 23, 2005 12:38 pm    Post subject: Reply with quote

i get an error with the new version. the closing tag isn't recognized. i have modified the search string in the prefilter.clicache.php file to this:

prefilter.clipcache.php:
Code:
$ld = "^";
$rd = "^";
$search = "\\{$ld}clipcache\s+(.*?)\s*\\{$rd}(.*)\\{$ld}/clipcache\\{$rd}";


index.tpl:
Code:
^clipcache id=top_image group=index ttl=10^<img src="^$IMAGES_DIR^index/1.jpg" width="768" height="166">^/clipcache^


can anybody tell me what i'am doing wrong?

thx, micha

BTW: i think there is an error in the prefilter.clipcache.php. check the params of the funtion smarty_core_write_file:

Code:
smarty_core_write_file(array('filename'=>$write_path . '/clipcache' . DIRECTORY_SEPARATOR . $file_name, 'contents'=>$clip_blocks[2][$i++], 'create_dirs'=>true), $compiler);


the "clipcache" folder needs to get a "/" - sign in front of it...
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Sep 23, 2005 9:03 pm    Post subject: Reply with quote

micha80 wrote:
i get an error with the new version. the closing tag isn't recognized. i have modified the search string in the prefilter.clicache.php file to this:

prefilter.clipcache.php:
Code:
$ld = "^";
$rd = "^";
$search = "\\{$ld}clipcache\s+(.*?)\s*\\{$rd}(.*)\\{$ld}/clipcache\\{$rd}";


index.tpl:
Code:
^clipcache id=top_image group=index ttl=10^<img src="^$IMAGES_DIR^index/1.jpg" width="768" height="166">^/clipcache^


can anybody tell me what i'am doing wrong?


Why would you do that? I indicated that the close tag must exactly equal the opening tag (including all params) except for the /. Otherwise you will have to build a much better parsing routine to handle nested cases. Further, you shouldn't mess with the delimiters. In fact, I have a new version that allows you to set delimiters in-template but I'm looking into ways to deal with the close tag syntax (again, its not easy without doing at least a stack based parser). Playing with delimiters in a fixed way like you have it is not a very good idea IMO. In the meantime, you shouldn't have problems with the 0.1.5 code if you match the tags as I suggest (and shown in my example). Do make sure you remove all of your compiled and cached files before testing Smile

micha80 wrote:
BTW: i think there is an error in the prefilter.clipcache.php. check the params of the funtion smarty_core_write_file:

Code:
smarty_core_write_file(array('filename'=>$write_path . '/clipcache' . DIRECTORY_SEPARATOR . $file_name, 'contents'=>$clip_blocks[2][$i++], 'create_dirs'=>true), $compiler);


the "clipcache" folder needs to get a "/" - sign in front of it...


Hmmm. That doesn't seem correct -- I have explicit code to do that very thing:[php:1:3903214d5e]
// write the clip block file source template
$write_path = $compiler->compile_dir;
if (preg_match('@[/\\\\]@', substr($write_path, -1)) === false) {
$write_path .= DIRECTORY_SEPARATOR;
} [/php:1:3903214d5e]

micha80 wrote:
've found the right condition to check with the is_cached function. you have to send the path (where the file is stored) and the filename to the function:

Code:
is_cached('/home/eigen/compile/clipcache/test.tpl#test', 'foo')


That would work but so should:

is_cached('test.tpl#test', 'foo')

since the clipcache dir is in the template_dir path array (as long as you register clipcache with the provided method in the sample class as I previously discussed).

I appreciate you testing this and it is giving me impetus to improve it. If you have any more suggestions, comments or fixes, please post them!

I should note, however, that this entire system is a little bit crazy. The sample template for example results in 11 files being accessed or created (the original template (1), a clipcache template for each of the two clipcache blocks (2), a cache file for each resulting template (3), a compile file for each template (3) and a dynamic include file for each clipcache block (2). This is actually no worse than if you wanted to do similar by hand (using explicit inlcudes and separate template files with a dynamically registered include plugin) but it does highlight the fact that it has a significant I/O effect. You really need to be using a php accelerator and probably a custom smarty cache handler (eg: the eaccelerator plugin at the wiki) with lots of ram to get any *potential* for speed benefits. Furthermore, this introduces some potentially difficult caching issues from the application side both because it becomes difficult to know when to send (and therefore create) a variable into a template and also because all of the clipcaches are regenerated everytime the main template is modified. There may also be concurrency issues on busy sites since templates are generated dynamically.

I think there are uses for this but I think it requires judicious planning and lots of performance testing before one decides to employ it for a specific implementation.

Best Regards.
Back to top
View user's profile Send private message
jason_ng
Smarty n00b


Joined: 28 Apr 2006
Posts: 1

PostPosted: Fri Apr 28, 2006 7:08 am    Post subject: BUG Reply with quote

From PHP Manual:
Quote:

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. preg_match() returns FALSE if an error occurred.


there is a bug in follow code(in 2 files):
[php:1:0973820c57]
if (preg_match('@[/\\\\]@', substr($write_path, -1)) === false) {
$write_path .= DIRECTORY_SEPARATOR;
}
[/php:1:0973820c57]

fix:

[php:1:0973820c57]
if (preg_match('@[/\\\\]@', substr($write_path, -1)) == false) {
$write_path .= DIRECTORY_SEPARATOR;
}
[/php:1:0973820c57]

OR

[php:1:0973820c57]
if (preg_match('@[/\\\\]@', substr($write_path, -1)) === 0) {
$write_path .= DIRECTORY_SEPARATOR;
}
[/php:1:0973820c57]

OR

use string rtrim ( string str [, string charlist] )


replace to follow code
[php:1:0973820c57]
$write_path = rtrim($write_path, "/\\").DIRECTORY_SEPARATOR;
[/php:1:0973820c57]

i prefer last!

BTW: what's difference between '@[/\\\\]@' AND '@[/\\]@'?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Apr 28, 2006 9:41 pm    Post subject: Reply with quote

@jason_ng:

Nice catch, thanks! The rtrim() method seems like the nicest way to do it as well. Good stuff.
Back to top
View user's profile Send private message
OpenMacNews
Smarty Rookie


Joined: 03 Aug 2004
Posts: 34
Location: Floating on Io's Methane Seas ...

PostPosted: Wed May 03, 2006 4:48 am    Post subject: stuck on getting clipcache setup properly Reply with quote

hi all.

i'm just migrating to a new server setup AND back to smarty after using a non-smarty CMS ...

i've been following along here, reading thru this thread, and am stuck on something probably something obvious /simple ...

fwiw, my env is:

apache 220/worker
php 512
eaccelerator trunk/r204
smarty 2.6.13
osx 10.4.6


now, after the requisite:

require('Smarty_ClipCache.class.php');
$smarty = new Smarty_ClipCache();

the presence of:

$smarty->register_clipcache();

fires the following error:

Fatal error: Smarty_ClipCache::require_once() [function.require]: Failed opening required ''
(include_path='/path/to/resources/includes:./:/usr/local/php_libs:blahblahblah') in /path/to/resources/includes/Smarty_ClipCache.class.php on line 30

checking,
ls -al resources/includes/Smarty_ClipCache.class.php
-rw-rw---- 1 www www 1434 2006-05-02 21:06 resources/includes/Smarty_ClipCache.class.php

& @ line 30:


27: function register_clipcache()
28: {
29: $this->load_filter('pre', 'clipcache');
30: require_once $this->_get_plugin_filepath('function', 'include_clipcache');
31: $this->register_function('include_clipcache', 'smarty_function_include_clipcache', false);
42: $write_path = $this->compile_dir;

i'm fairly sure i've got all my path-ing right, as my *own* plugins *are* found in resources/plugins/

any suggestion as to what i might be missing?

thx!

richard
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 03, 2006 4:18 pm    Post subject: Reply with quote

Hi. Is that the exact error message? Smarty_ClipCache::require_once() isn't a method of that class... Anyways, the code is known to work when you have the two included plugins (eg: function.include_clipcache.php) in your plugins directory as shown in the code listing. I can't really tell why it isn't for you -- sorry!
Back to top
View user's profile Send private message
OpenMacNews
Smarty Rookie


Joined: 03 Aug 2004
Posts: 34
Location: Floating on Io's Methane Seas ...

PostPosted: Wed May 03, 2006 4:28 pm    Post subject: Reply with quote

hi boots
boots wrote:
Hi. Is that the exact error message?

except for the obvious

"/path/to/resources/" & "blahblahblah", yes, that error is exact & reproducible.

boots wrote:
Smarty_ClipCache::require_once() isn't a method of that class...


hence part of my confusion :-/

all other plugins etc are working, so i don't think its paths/perms ...

i've of course copied-n-pasted the source from the thread here.

wondering if there have been any subsequent tweaks that i've missed ...

do you, perchance, have a 'latest' all in one place listing of the current src for the plugins etc?

dunno what else to check here ...
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 03, 2006 4:52 pm    Post subject: Reply with quote

@OpenMacNews: I updated the code (I replaced the code shown in the previous page's post) to version 0.1.6. It is not well tested but should work as it mainly only contains small bug fixes. Let me know how it works out for you.
Back to top
View user's profile Send private message
OpenMacNews
Smarty Rookie


Joined: 03 Aug 2004
Posts: 34
Location: Floating on Io's Methane Seas ...

PostPosted: Wed May 03, 2006 10:07 pm    Post subject: Reply with quote

boots wrote:
@OpenMacNews: I updated the code (I replaced the code shown in the previous page's post) to version 0.1.6. It is not well tested but should work as it mainly only contains small bug fixes. Let me know how it works out for you.


hi boots,

with the 0.1.6 changes, the (re)addition of:

$smarty = new Smarty_ClipCache();
+++ $smarty->register_clipcache();

to the parent 'test.php' no longer fires an error, however, in *template*:

<ste: clipcache id=test group=foo ttl=5 ldelim=<ste: rdelim=:ste> :ste>
BLAH BLAH BLAH
<ste: /clipcache id=test group=foo ttl=5 ldelim=<ste: rdelim=:ste> :ste>

causes an error:

Fatal error: Smarty error: [in test.tpl line 59]: syntax error: unrecognized tag 'clipcache' (Smarty_Compiler.class.php, line 580) in /path/to/Smarty.class.php on line 1095

which, i'm guessingm may be a problem with my non-standard delims of '<ste:' & ':ste>', though not sure. wan't entirely clear to me if the prior page's delim def'n support was 'in there' already ...

cheers,

richard
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 -> Feature Requests All times are GMT
Goto page Previous  1, 2, 3, 4  Next
Page 3 of 4

 
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