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

Smarty function cycle, why dynamically?

 
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
Stitch
Smarty n00b


Joined: 06 Nov 2006
Posts: 3

PostPosted: Mon Nov 06, 2006 12:09 pm    Post subject: Smarty function cycle, why dynamically? Reply with quote

Hi there,

I am writing a couple of sites and use the smarty template engine. It's really nice but i wonder why some things happen that will slow down presentation of some pages.

One of these wonderings is; Why is smarty_function_cycle used after the template is compiled? One could just as easily hard-code the colors/classes into the compiled template. A compiled template won't be edited by the developer anyway; so why the wastefull calls to the cycle function?

On an average example page i see about 25 calls to this function taking up 15 miliseconds on every request!

I don't think i can use the caching because i work with a lot of database code and userinput.

Regards,
Stitch
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Nov 08, 2006 2:43 am    Post subject: Reply with quote

Hi Stitch.

First of all, this is not a bug, so welcome to the Feature Request forum Wink

Secondly, what do you mean about hard-coding values? Say you have a loop and you want on every iteration to alternate items. You can do it in many ways. For example, you can use something like:
Code:
{foreach name=foo from=$loop item=item}
    {if $smarty.foreach.foo.iteration is odd}
        odd
    {else}
         even
    {/if}
{/foreach}

or you can use cycle:
Code:
{foreach from=$loop item=item}
    {cycle values="odd,even"}
{/foreach}

Now while each iteration results in a function call to cycle, in the previous case, there is only an if test. Still, cylce is much easier for a designer to write and understand (and get correct).

So how to hard-bake it in? By inserting an array of values and a counter in-place? There is a problem with that -- you would have to traditionally define the values array you want and the iteration counter before you enter the loop. Smarty doesn't do semantical processing like that. By the time it finds out about the cycle, it is already in the loop -- but even then, it doesn't really "know". Now you can probably do an if-test to see if our cycle var was created to avoid recreating it.

eg (pseudo code):
Code:

foreach( $loop as $item ) {
    if(!isset( $counters['every counter will need to be tracked'] ) {
         $_id = get_smarty_counter_id(); // all counters must now be tracked
         $counters[$_id] = array('values'=>array('odd', 'even'), 'index'=>0, 'count'=>2);
    } else {
       $counters[$_id]['index']++;
       $counters[$_id]['index'] = ( $counters[$_id]['index'] = $counters[$_id]['count'] ) ? 0 : $counters[$_id]['count'];
    }
    echo $counters[$_id]['values'][$counters[$_id]['index']];
}

Of course, that doesn't consider {cycle values=$passed_in_values} where $passed_in_values can change with each invovation of the template -- not just when it is first compiled.

BUT, assuming I haven't missed your point or something obvious about cycle, it is conceivable that something like the above can be as a compiler plugin -- at least for "static" value cases of cycle. Maybe someone will want to take that on.
Back to top
View user's profile Send private message
Stitch
Smarty n00b


Joined: 06 Nov 2006
Posts: 3

PostPosted: Wed Nov 08, 2006 1:45 pm    Post subject: Reply with quote

boots wrote:
Hi Stitch.

First of all, this is not a bug, so welcome to the Feature Request forum Wink


Sorry for that... Embarassed
The following is just a show that im not into core development / knowledge of smarty, but it might be usefull.

I thought {cycle} was optimised to run once and not to be rewritten to php code.
Since {cycle} is a lot easier to use than something like
Code:

{php}($i%2 == 0 ? echo 'odd' : echo 'even');$i++;{/php}


To hard-bake it in on compilation; i guess a function is found somewhere and translated to the real php function name + params?
If so, this would be possible on compilation;
Code:

$funcname = "smarty_function_".$function_name;
require....
print $funcname($params,$this)?

But you don't want that with a number of other plugins? I also guess it would go wrong on the $this part.


But anyway; i think i will now just semi-hard code the function like this and call it with a {php} block;
Code:

// pseudo code :) the % probably wont work in the return.
class cycle{
    static $i = 0;

    __constuct(array $colors){
       $count = count($colors);
       $this->i++;
       return $colors[$this->i % $count];
    }
}


boots wrote:

it is conceivable that something like the above can be as a compiler plugin -- at least for "static" value cases of cycle. Maybe someone will want to take that on.

Hope someone does... Smile i probably won't/can't

ps; i'm sorry my post is somewhat chaotic, it's been a long day.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Nov 08, 2006 5:09 pm    Post subject: Reply with quote

Never use {php} blocks! They are bad form and leave you with a situation that may be worse than just straight PHP to begin with.

The question is, which costs you more: development time or deployment? Chances are good that it is the former meaning that {cycle} and the cost of added function calls aren't all that bad.

Just my 2c.
Back to top
View user's profile Send private message
Stitch
Smarty n00b


Joined: 06 Nov 2006
Posts: 3

PostPosted: Fri Nov 10, 2006 8:16 am    Post subject: Reply with quote

boots wrote:
Never use {php} blocks! They are bad form and leave you with a situation that may be worse than just straight PHP to begin with.

The question is, which costs you more: development time or deployment? Chances are good that it is the former meaning that {cycle} and the cost of added function calls aren't all that bad.

Just my 2c.


Ok Smile As long as one is separating display logic from the application logic {php} isn't that bad (smarty can't do all). If a customer wants to have the application(s) to be faster ill look into the cycle discussion again.

Thnx for the comments and your thoughts.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Nov 10, 2006 2:56 pm    Post subject: Reply with quote

Take a look at the compiled template, you'll see how the {cycle} function is handled in PHP, as with any other plugin.
Back to top
View user's profile Send private message Visit poster's website
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Mon Nov 13, 2006 8:49 pm    Post subject: Reply with quote

If you are interessted in speeding up your website, use caching and don't make up your mind about those minor things Wink
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
Page 1 of 1

 
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