View previous topic :: View next topic |
Author |
Message |
h1nebefe Smarty Rookie
Joined: 20 Apr 2003 Posts: 7
|
Posted: Sun Apr 20, 2003 5:01 pm Post subject: Block-function {counter} |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Apr 20, 2003 11:03 pm Post subject: |
|
|
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 |
|
h1nebefe Smarty Rookie
Joined: 20 Apr 2003 Posts: 7
|
Posted: Sun Apr 20, 2003 11:13 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Apr 21, 2003 8:30 am Post subject: custom block function : loop |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Apr 21, 2003 8:40 am Post subject: |
|
|
hmm. {section loop=$numpages} would do. with $numpages being a number. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Apr 21, 2003 8:57 am Post subject: |
|
|
messju wrote: | hmm. {section loop=$numpages} would do. with $numpages being a number. |
well that's much simpler Would you believe it never occured to me to try to use section like that? I'm to stuck on foreach I guess
To think messju is suggesting {section} |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Apr 21, 2003 11:16 am Post subject: |
|
|
i admitted recently "i prefer {foreach} *whereever i can*" but this is i case where i cannot |
|
Back to top |
|
h1nebefe Smarty Rookie
Joined: 20 Apr 2003 Posts: 7
|
Posted: Mon Apr 21, 2003 11:23 am Post subject: |
|
|
Thanks for the tip, messju! |
|
Back to top |
|
regnastor Smarty Rookie
Joined: 25 May 2003 Posts: 16
|
Posted: Sun Jun 01, 2003 4:51 pm Post subject: helpful |
|
|
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 |
|
Doomer Smarty Rookie
Joined: 16 Jun 2003 Posts: 12 Location: Germany/Bavaria/BT
|
Posted: Sun Aug 31, 2003 6:22 pm Post subject: A little problem with an array ! |
|
|
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 |
|
|