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

Block-function {counter}

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
h1nebefe
Smarty Rookie


Joined: 20 Apr 2003
Posts: 7

PostPosted: Sun Apr 20, 2003 5:01 pm    Post subject: Block-function {counter} Reply with quote

I would like to be able to easily make page numbers in any format. I couldn't find any function in the docs for doing this, so I am requesting it here.

This is an example of what I mean:

{counter from=1 to=$pagenumber item=number}
<a href="mypage.php?p={$number}">{$number}</a>
{/counter}

DocUK
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


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

PostPosted: Sun Apr 20, 2003 11:03 pm    Post subject: Reply with quote

Hi.

Yep, no generic loops in Smarty.

Is it possible that you can assign this from PHP as an array so that you can use {foreach} or {section}?

test.php

Code:
<?php
$smarty=new Smarty;
$counter=array();
for ($i=1; $i<= 10; $counter[] = $i++);
$smarty->assign('pages',  $counter);
$smarty->display('test.tpl');
?>


test.tpl

Code:
{section name=number loop=$pages}
    <a href="mypage.php?p={$pages[number]}">{$number}</a>
{/section}


Yes, creating a list like this to iterate over is inefficient for large lists, but look how the details of the looping are taken out of the template code--a real plus when you have different people doing template design/building and application design/building.
Back to top
View user's profile Send private message
h1nebefe
Smarty Rookie


Joined: 20 Apr 2003
Posts: 7

PostPosted: Sun Apr 20, 2003 11:13 pm    Post subject: Reply with quote

I realized that before posting. As you said, however, this is very inefficient. What you stated is what I am currently using as a temporary solution. Thanks for the reply, though!
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


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

PostPosted: Mon Apr 21, 2003 8:30 am    Post subject: custom block function : loop Reply with quote

I just read messju's interesting post on the smarty-dev mailing list concerning the addition of the $repeat parameter. Clever indeed, as it gives blocks the ability to maintain internal loop counters.

Here is a simple plugin to implement your counter. I haven't benched the performance of the interface, but hey, it works!

This requires the CVS version of Smarty.

block.loop.php

Code:
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty loop block plugin
 *
 * Type:     loop function<br>
 * Name:     loop<br>
 * Purpose:  generic looping block
 */
function smarty_block_loop($params, $content, &$this, &$repeat)
   {
   // accept only 'name', 'from', 'to' and 'item' as params

   $name = $params['name'];
   $from = $params['from'];
   $to   = $params['to'];
   $item = $params['item'];

   if ($this->block_loop[$name]['looping'] == True)
      {
      $current = $this->block_loop[$name]['current'];
      }
   else
      {
      $this->block_loop[$name]['looping'] = True;
      $this->block_loop[$name]['from'] = $from;
      $this->block_loop[$name]['to'] = $to;
      $this->block_loop[$name]['item'] = $item;
      $this->block_loop[$name]['current'] = $from;
      $current = --$from;
      }
   $current++;
   $this->assign($item, $current);
   $this->block_loop[$name]['current'] = $current;

   $repeat = ($current <= $to) ? True : False;
   $this->block_loop[$name]['looping']  = ($current <= $to) ? True : False;

   return $content;
   }

?>


sample.tpl

Code:
{loop name=test from=1 to=10 item=number}
     <a href="mypage.php?p={$number}">{$number}</a>
{/loop}


Please, don't even think of using this code for production. It WILL break and except for demonstrating the purpose, it is just a quick hack. I hope it gets the ball rolling for you.

Good luck and Caveat Emptor!
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Apr 21, 2003 8:40 am    Post subject: Reply with quote

hmm. {section loop=$numpages} would do. with $numpages being a number.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


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

PostPosted: Mon Apr 21, 2003 8:57 am    Post subject: Reply with quote

messju wrote:
hmm. {section loop=$numpages} would do. with $numpages being a number.


well that's much simpler Smile Would you believe it never occured to me to try to use section like that? I'm to stuck on foreach I guess Laughing

To think messju is suggesting {section} Wink
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Apr 21, 2003 11:16 am    Post subject: Reply with quote

i admitted recently "i prefer {foreach} *whereever i can*" but this is i case where i cannot Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
h1nebefe
Smarty Rookie


Joined: 20 Apr 2003
Posts: 7

PostPosted: Mon Apr 21, 2003 11:23 am    Post subject: Reply with quote

Thanks for the tip, messju!
Back to top
View user's profile Send private message Send e-mail
regnastor
Smarty Rookie


Joined: 25 May 2003
Posts: 16

PostPosted: Sun Jun 01, 2003 4:51 pm    Post subject: helpful Reply with quote

Hi,
very helpful topic and it was luck that i stumbled over it. I think it's very unclear in the Documentation. It would help a lot if it would be mentioned somewhere in the docs that you can use an integer for the attribute loop.
Back to top
View user's profile Send private message
Doomer
Smarty Rookie


Joined: 16 Jun 2003
Posts: 12
Location: Germany/Bavaria/BT

PostPosted: Sun Aug 31, 2003 6:22 pm    Post subject: A little problem with an array ! Reply with quote

Ok first the php discribtion.

I read read a directiory for pictures and would give it out in a table


Code:

<table>
  <tr><td>{pic 1,3,5,7...}</td><td>{pic 2,4,6,8...}</td></tr>
</table>



i tryed this but it didnīt work

Code:

<table>
{foreach from=$inhalt_array item=inhalt key=id}
  <tr><td>{$inhalt[index_next]}</td><td>{$inhalt[index_next]}</td></tr>
{/foreach}
</table>


Please help me with this litte problem !
Thanks DOOMER

-----------
GERMANY[/code]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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 -> Tips and Tricks 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