First step to solve {nocache} {/nocache} problematic
| From: | Andr� Rabold | Date: | Wed, 22 Jan 2003 08:29:05 +0000 |
| Subject: | First step to solve {nocache} {/nocache} problematic | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-583@lists.php.net to get a copy of this message | ||
Hi folks,
I have tried to solve the {nocache} {/nocache} problematic. Many users
requested a block
function which's content gets never cached. This would make template
development using
caching a lot easier than splitting them in hundreds of pieces and then to
use {insert} tags.
At the end of this document you find two patches. DO NOT USE THEM!
Using these patches Smarty will recognize {nocache} ... {/nocache} block
tags. They will be compiled into <?php /* NOCACHE */ eval(...) ?>.
Even in the cached template these special PHP tags will remain. While
displaying the cached templated the evals get executed. This already works
very well.
One problem is still to solve: At runtime (of the cached templated) I have
no information about needed plugins so all function calls ({foo bar="blah"})
will fail.
So we need a functionality which inserts into the cached template some
informations about needed plugins. This is still to do. There are also some
other bugs I think. But the good thing is that the new {nocache} tag doesn't
break any current Smarty syntax.
What do you think? Is this a good approach?
Any comments?
André
Index: Smarty.class.php
===================================================================
RCS file: /repository/smarty/Smarty.class.php,v
retrieving revision 1.329
diff -r1.329 Smarty.class.php
648a649,654
> if (preg_match_all('/\<\?php \/\* NOCACHE \*\/ echo eval\("(.*?)"\);
\?>/si', $_smarty_results, $matches)) {
> foreach ($matches[1] as $key => $code) {
> $matches[1][$key] = eval($code);
> }
> $_smarty_results = str_replace($matches[0], $matches[1],
$_smarty_results);
> }
1412a1419,1439
> }
> }
>
> /*======================================================================*\
> Function: _run_nocache_handler
> Purpose: Handle nocache tags
> \*======================================================================*/
> function _run_nocache_handler($args, $content)
> {
> if ($this->debugging) {
> $debug_start_time = $this->_get_microtime();
> }
>
> $ques="?";
> $php_open="<${ques}php";
> $php_close="${ques}>";
> if ($this->caching) {
> $evalcode = $php_close.$content.$php_open." ";
> return "$php_open /* NOCACHE */ echo eval(\"".$evalcode."\");
> $php_close";
> } else {
> return eval($php_close.$content.$php_open." ");
Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/Smarty_Compiler.class.php,v
retrieving revision 1.148
diff -r1.148 Smarty_Compiler.class.php
269c269,274
< $compiled_tags[] = $this->_compile_tag($template_tags[$i]);
---
> if ($this->_nocache_tag && $template_tags[$i] != "/nocache") {
> $compiled_tags[] =
addcslashes($this->_compile_tag($template_tags[$i]),"$\\");
> }
> else {
> $compiled_tags[] = $this->_compile_tag($template_tags[$i]);
> }
443a449,454
> case 'nocache':
> return $this->_compile_nocache_starttag($tag_args);
>
> case '/nocache':
> return $this->_compile_nocache_endtag($tag_args);
>
664a676,696
> /*======================================================================*\
> Function: _compile_nocache_starttag
> Purpose: Compile {nocache}...{/nocache} tag
> \*======================================================================*/
> function _compile_nocache_starttag($tag_args)
> {
> $this->_nocache_tag = true;
> return "<?php \$_NOCACHE_CONTENT = <<<NOCACHE\n";
> }
>
> /*======================================================================*\
> Function: _compile_nocache_starttag
> Purpose: Compile {nocache}...{/nocache} tag
> \*======================================================================*/
> function _compile_nocache_endtag($tag_args)
> {
> $this->_nocache_tag = false;
> $content = "\nNOCACHE;\n".
> "echo \$this->_run_nocache_handler(array(".implode(', ',
(array)$arg_list)."),\$_NOCACHE_CONTENT); ?>\n";
> return $content;
> }