Re: Here it is: Patch for {nocache} support
| From: | Andr� Rabold | Date: | Wed, 16 Apr 2003 07:04:43 +0000 |
| Subject: | Re: Here it is: Patch for {nocache} support | ||
| References: | 1 | Groups: | php.smarty.dev |
| Request: | Send a blank email to php-smarty-dev+get-1182@lists.php.net to get a copy of this message | ||
Here's an updated version :-)
* I removed the support for "nocache" modifier because it is not really
needed and makes things only complicated.
* Because designers shouldn't need to care about caching I have added a
third parameter to the register_function and register_block functions:
register_function($function, $function_imp, $cached = true)
register_block($block, $block_imp, $cached = true)
If the $cached flag is set to TRUE (default) the function behaves just
normal. If you set it to FALSE the function won't get cached anymore. If
it's a block function even the whole body won't be cached.
* The "nocache" block function is no more ... long live the "nocache" block
function ;-)
I have removed the built-in nocache function because you don't need it
anymore and because it's not a very good programming style ;-)
If you really need it create a new custom function "nocache" and register it
in smarty as in the following example.
So it's time to do some optimizations I think ;-)
bye,
André
Example functions
===================================================================
// {nocache}...{/nocache} block
function smarty_block_nocache($params, $content, &$smarty) {
return $content;
}
// print current date and time (never cached)
function smarty_function_currenttime($params, &$smarty) {
return "Current Date and Time: ".date("Y-m-d H:i:s");
}
// Register custom functions
$smarty->register_block("nocache", "smarty_block_nocache", false);
$smarty->register_function("currenttime", "smarty_function_currenttime",
false);
Index: Smarty.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty.class.php,v
retrieving revision 1.375
diff -r1.375 Smarty.class.php
684c684
< function register_function($function, $function_impl)
---
> function register_function($function, $function_impl, $cached = true)
687c687
< array($function_impl, null, null, false);
---
> array($function_impl, null, null, false, $cached);
733c733
< function register_block($block, $block_impl)
---
> function register_block($block, $block_impl, $cached = true)
736c736
< array($block_impl, null, null, false);
---
> array($block_impl, null, null, false, $cached);
1107a1108
> $_smarty_results =
$this->_eval_cached_template($_smarty_results);
1176a1178
> $_smarty_results =
$this->_eval_cached_template($_smarty_results);
1890d1891
<
1893c1894,1906
<
---
>
> function _eval_cached_template($results) {
> $cacheSmarty = $this; // create a copy of current Smarty object
> $cacheSmarty->left_delimiter = "<!--{#SMARTYCACHE#";
> $cacheSmarty->right_delimiter = "}-->";
> $cacheSmarty->caching = 0;
> $cacheSmarty->_compile_template("evaluated template", $results,
$source);
> ob_start();
> eval('?>' . $source);
> $results = ob_get_contents();
> ob_end_clean();
> return $results;
> }
Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty_Compiler.class.php,v
retrieving revision 1.204
diff -r1.204 Smarty_Compiler.class.php
76a77,81
>
> var $_cache_left_delimiter = "<!--{#SMARTYCACHE#";
> var $_cache_right_delimiter = "}-->";
> var $_nocache_stack = array();
>
388a394,400
> if ($this->caching && count($this->_nocache_stack) > 0) {
> if (!(isset($this->_plugins['block'][$tag_command]) &&
> $this->_plugins['block'][$tag_command][4] === false) &&
>
("/".$this->_nocache_stack[count($this->_nocache_stack)-1]) != $tag_command)
{
> return
$this->_cache_left_delimiter.$template_tag.$this->_cache_right_delimiter;
> }
> }
579a592,608
> /*
> * Caching
> * Check if the current block function was registered as cached or
not.
> */
> if ($this->caching &&
isset($this->_plugins['block'][$tag_command])) {
> if ($this->_plugins['block'][$tag_command][4] === false) {
> if ($start_tag) {
> array_push($this->_nocache_stack, $tag_command);
> }
> else {
> array_pop($this->_nocache_stack);
> }
> $output .= $this->_cache_left_delimiter .
($start_tag?"":"/") . $tag_command.$tag_modifier . " " .
$tag_args.$this->_cache_right_delimiter;
> return true;
> }
> }
>
639,644c668,673
< $out_tag_text =
"\$this->_plugins['block']['$tag_command'][0](\$this->_tag_stack[count(\$thi
s->_tag_stack)-1][1], \$this->_block_content, \$this)";
< if($tag_modifier != '') {
< $this->_parse_modifiers($out_tag_text, $tag_modifier);
< }
< $output .= 'echo ' . $out_tag_text . ';';
< $output .= " array_pop(\$this->_tag_stack); ?>";
---
> $out_tag_text =
"\$this->_plugins['block']['$tag_command'][0](\$this->_tag_stack[count(\$thi
s->_tag_stack)-1][1], \$this->_block_content, \$this)";
> if($tag_modifier != '') {
> $this->_parse_modifiers($out_tag_text, $tag_modifier);
> }
> $output .= 'echo ' . $out_tag_text . ';';
> $output .= " array_pop(\$this->_tag_stack); ?>";
659a689,694
> if ($this->caching &&
isset($this->_plugins['function'][$tag_command])) {
> if ($this->_plugins['function'][$tag_command][4] === false) {
> return $this->_cache_left_delimiter . $tag_command .
$tag_modifier . " " . $tag_args.$this->_cache_right_delimiter;
> }
> }
>
661c696
<
---
>
664c699
<
---
>
672,678c707,713
<
< $return =
"\$this->_plugins['function']['$tag_command'][0](array(".implode(',',
(array)$arg_list)."), \$this)";
<
< if($tag_modifier != '') {
< $this->_parse_modifiers($return, $tag_modifier);
< }
<
---
>
> $return =
"\$this->_plugins['function']['$tag_command'][0](array(".implode(',',
(array)$arg_list)."), \$this)";
>
> if($tag_modifier != '') {
> $this->_parse_modifiers($return, $tag_modifier);
> }
>
679a715
>
begin 666 nocache-patch2.txt
M26YD97@Z(%-M87)T>2YC;&%S<RYP:' -/3T]/3T]/3T]/3T]/3T]/3T]/3T]
M/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]
M/0U20U,@9FEL93H@+W)E<&]S:71O<GDO<VUA<G1Y+VQI8G,O4VUA<G1Y+F-L
M87-S+G!H<"QV#7)E=')I979I;F<@<F5V:7-I;VX@,2XS-S4-9&EF9B M<C$N
M,S<U(%-M87)T>2YC;&%S<RYP:' --C@T8S8X- T\(" @("!F=6YC=&EO;B!R
M96=I<W1E<E]F=6YC=&EO;B@D9G5N8W1I;VXL("1F=6YC=&EO;E]I;7!L*0TM
M+2T-/B @(" @9G5N8W1I;VX@<F5G:7-T97)?9G5N8W1I;VXH)&9U;F-T:6]N
M+" D9G5N8W1I;VY?:6UP;"P@)&-A8VAE9" ]('1R=64I#38X-V,V.#<-/" @
M(" @(" @("
@("!A<G)A>2@D9G5N8W1I;VY?:6UP;"P@;G5L;"P@;G5L;"P@
M9F%L<V4I.PTM+2T-/B @(" @(" @(" @("!A<G)A>2@D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QO8VLG75LD=&%G7V-O;6UA
M;F1=6S1=(#T]/2!F86QS92D@)B8-/B @(" @(" @(" @(" @(" @*"(O(BXD
M=&AI<RT^7VYO8V%C:&5?<W1A8VM;8V]U;G0H)'1H:7,M/E]N;V-A8VAE7W-T
M86-K*2TQ72D@(3T@)'1A9U]C;VUM86YD*2![#3X@(" @(" @(" @(" @(" @
M(" @("!R971U<FX@)'1H:7,M/E]C86-H95]L969T7V1E;&EM:71E<BXD=&5M
M<&QA=&5?=&%G+B1T:&ES+3Y?8V%C:&5?<FEG:'1?9&5L:6UI=&5R.PT^("
@
M(" @(" @(" @('T-/B @(" @(" @('T--3<Y834Y,BPV,#@-/B @("
@(" @
M("\J#3X@(" @(" @(" @*B!#86-H:6YG#3X@(" @(" @(" @*B!#:&5C:R!I
M9B!T:&4@8W5R<F5N="!B;&]C:R!F=6YC=&EO;B!W87,@<F5G:7-T97)E9"!A
M<R!C86-H960@;W(@;F]T+@T^(" @(" @(" @("HO( T^(" @(" @(" @:68@
M*"1T:&ES+3YC86-H:6YG("8F(&ES<V5T*"1T:&ES+3Y?<&QU9VEN<ULG8FQO
M8VLG75LD=&%G7V-O;6UA;F1=*2D@>PT^(" @(" @(" @("
@(&EF("@D=&AI
M<RT^7W!L=6=I;G-;)V)L;V-K)UU;)'1A9U]C;VUM86YD75LT72 ]/3T@9F%L
M<V4I('L-/B @(" @(" @(" @(" @("
@:68@*"1S=&%R=%]T86<I('L-/B @
M(" @(" @(" @(" @(" @("
@(&%R<F%Y7W!U<V@H)'1H:7,M/E]N;V-A8VAE
M7W-T86-K+" D=&%G7V-O;6UA;F0I.PT^(" @(" @(" @(" @(" @("!]#3X@
M(" @(" @(" @(" @(" @(&5L<V4@>PT^(" @(" @("
@(" @(" @(" @(" @
M87)R87E?<&]P*"1T:&ES+3Y?;F]C86-H95]S=&%C:RD[#3X@(" @(" @(" @
M(" @(" @('T-/B @(" @(" @(" @(" @("
@)&]U='!U=" N/2 D=&AI<RT^
M7V-A8VA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@D
M;W5T7W1A9U]T97AT+" D=&%G7VUO9&EF:65R*3L-/B @(" @(" @(" @("!]
M#3X@(" @(" @(" @(" @)&]U='!U=" N/2 G96-H;R
G("X@)&]U=%]T86=?
M=&5X=" N("<[)SL-/B @(" @(" @(" @("
D;W5T<'5T("X]("(@87)R87E?
M<&]P*%PD=&AI<RT^7W1A9U]S=&%C:RD[(#\^(CL--C4Y838X.2PV.30-/B @
M(" @(" @(&EF("@D=&AI<RT^8V%C:&EN9R
F)B!I<W-E="@D=&AI<RT^7W!L
M=6=I;G-;)V9U;F-T:6]N)UU;)'1A9U]C;VUM86YD72DI('L-/B @(" @(" @
M(" @("!I9B H)'1H:7,M/E]P;'5G:6YS6R=F=6YC=&EO;B==6R1T86=?8V]M
M;6%N9%U;-%T@/3T](&9A;'-E*2![#3X@(" @(" @(" @(" @("
@(')E='5R
M;B D=&AI<RT^7V-A8VAE7VQE9G1?9&5L:6UI=&5R("X@)'1A9U]C;VUM86YD
M("X@)'1A9U]M;V1I9FEE<B N("(@(B N("1T86=?87)G<RXD=&AI<RT^7V-A
M8VAE7W)I9VAT7V1E;&EM:71E<CL-/B @(" @(" @(" @("!]#3X@(" @(" @
M("!]#3X@(" @(" @(" --C8Q8S8Y-@T\( TM+2T-/B @(" @(" @( TV-C1C
M-CDY#3P@#2TM+0T^(" @(" @(" @#38W,BPV-SAC-S W+#<Q,PT\( D)#3P@
M"0DD<F5T=7)N(#T@(EPD=&AI<RT^7W!L=6=I;G-;)V9U;F-T:6]N)UU;)R1T
M86=?8V]M;6%N9"==6S!=*&%R<F%Y*"(N:6UP;&]D92@G+"<L("AA<G)A>2DD
M87)G7VQI<W0I+B(I+"!<)'1H:7,I(CL-/" )"0T\( D):68H)'1A9U]M;V1I
M9FEE<B A/2 G)RD@>PT\( D)"21T:&ES+3Y?<&%R<V5?;6]D:69I97)S*"1R
M971U<FXL("1T86=?;6]D:69I97(I.PT\( D)?0T\( D)#2TM+0T^(" @(" @
M(" @#3X@(" @(" @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