View previous topic :: View next topic |
Author |
Message |
giksos Smarty n00b
Joined: 23 Mar 2006 Posts: 3
|
Posted: Sat Mar 25, 2006 3:52 am Post subject: while-loop plugin |
|
|
The ability to do a while-loop is a requested functionality in Smarty. Till it gets implemented in the core, one can use the following plugin:
Code: |
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {while} block function plugin
*
* Type: block function<br>
* Name: while<br>
* Date: Mar 23 2006<br>
* Purpose: provide a while-loop<br>
* Input: var = string, name of a boolean variable
* Examples: The following code will loop 1 time ;)
* <pre>
* {assign var="repeat" value=true}
* {while var="repeat"}
* hello world
* {assign var="repeat" value=false}
* {/while}
* </pre>
* @author Misha Aizatulin <avatar@hot.ee>
*/
function smarty_block_while($params, $content, &$smarty, &$repeat)
{
if(!isset($params['var']))
$smarty->trigger_error("while: missing parameter 'var'", E_USER_ERROR);
// we have to take the name of the variable instead of
// the variable itself, because parameters to blocks are
// computed only once
$repeat = $smarty->get_template_vars($params['var']);
return $content;
}
?>
|
Misha
P.S. This finally makes smarty a turing-complete language
P.P.S. Improvement suggestion for the forum: print code in fixed-width font! |
|
Back to top |
|
lordlamer Smarty n00b
Joined: 06 Sep 2006 Posts: 4
|
Posted: Wed Sep 06, 2006 9:43 pm Post subject: |
|
|
Hello,
i am searching for a while and a for function in smarty and i found your while function here. I think it is a good beginning but not so powerful like "if".
Is it possible to create a {while conditions}do{/while} and to use the internal {if} in smarty for the confitions? It would be great if somebody can give me tips how i can do that. All i want is to use the same conditions that are possible for "if".
regards,
Frank Habermann |
|
Back to top |
|
giksos Smarty n00b
Joined: 23 Mar 2006 Posts: 3
|
Posted: Wed Sep 06, 2006 11:10 pm Post subject: |
|
|
lordlamer wrote: |
Is it possible to create a {while conditions}do{/while} and to use the internal {if} in smarty for the confitions? It would be great if somebody can give me tips how i can do that. All i want is to use the same conditions that are possible for "if".
|
I'm afraid it is not possible, but I may be wrong, cause I haven't used smarty for a while. You should ask the authors of smarty whether the native while ever gets implemented. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Sep 07, 2006 1:16 am Post subject: |
|
|
You probably could if you did it as a compiler plugin. Will Smarty ever natively support it? I doubt it. For myself, I already think there is already one-too-many looping styles in Smarty (I'm looking at you, section). |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Sep 07, 2006 9:49 am Post subject: |
|
|
giksos wrote: | You should ask the authors of smarty whether the native while ever gets implemented. |
I doubt it will. |
|
Back to top |
|
lordlamer Smarty n00b
Joined: 06 Sep 2006 Posts: 4
|
Posted: Thu Sep 07, 2006 9:16 pm Post subject: |
|
|
Hi,
here is my solution for a while function that uses the conditions from if
Code: |
compiler.while.php
<?php
function smarty_compiler_while($tag_arg, &$smarty) {
$res = $smarty->_compile_if_tag($tag_arg);
preg_match("/<\?php if (.*): \?>/",$res,$token);
return "while " . $token[1] . " {";
}
?>
compiler.endwhile.php
<?php
function smarty_compiler_endwhile($tag_arg, &$smarty) {
return "}";
}
?>
example:
{assign var="test" value=1}
{while ($test <= 5)}
{assign var="test" value="`$test+1`"}
jo
{endwhile}
|
|
|
Back to top |
|
lordlamer Smarty n00b
Joined: 06 Sep 2006 Posts: 4
|
Posted: Thu Sep 07, 2006 9:23 pm Post subject: |
|
|
for your information
i use the function endwhile because i dont know how to make it with /while. if anybody has idea he is welcome
regards,
Frank |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Sep 07, 2006 11:19 pm Post subject: |
|
|
@lorlamer: Not too difficult once you know the technique for it (or should I say idiosyncratic behaviour of it )
1) Put both functions in your compiler.while.php
2) after the function declarations, add something like:
[php:1:eda6c48e38]$this->register_compiler_function('/while', 'smarty_compiler_endwhile');[/php:1:eda6c48e38]
So....when {while} is used, Smarty will:
i) load compiler.while.php
ii) which also contains smarty_compiler_endwhile
ii) and also contains the requisite manual registration for the endwhile using the "/while" notation.
Note that {endwhile} will *not* be available since it won't be registered and isn't loadable (since it isn't in its own compiler.endwhile.php file). You can have it both ways with slightly more effort, but I don't think that is a good idea.
HTH |
|
Back to top |
|
lordlamer Smarty n00b
Joined: 06 Sep 2006 Posts: 4
|
Posted: Fri Sep 08, 2006 9:38 am Post subject: |
|
|
and here is the complete class
@boots: thanks for the hints
regards,
Frank
Code: |
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {while} compiler function plugin
*
* Type: compiler function<br>
* Name: while<br>
* Date: Sep 08 2006<br>
* Purpose: provide a while-loop<br>
* @author Frank Habermann <lordlamer at lordlamer dot de>
* @param string containing var-attribute and value-attribute
* @param Smarty_Compiler
* Examples: The following code will loop 5 times ;)
* <pre>
* {assign var="test" value=1}
* {while ($test <= 5)}
* {assign var="test" value="`$test+1`"}
* jo
* {/while}
* </pre>
*/
$this->register_compiler_function('/while', 'smarty_compiler_endwhile');
function smarty_compiler_while($tag_arg, &$smarty) {
$res = $smarty->_compile_if_tag($tag_arg);
preg_match("/<\?php if (.*): \?>/",$res,$token);
return "while " . $token[1] . " {";
}
function smarty_compiler_endwhile($tag_arg, &$smarty) {
return "}";
}
?>
|
|
|
Back to top |
|
ITF Smarty n00b
Joined: 17 Dec 2009 Posts: 1
|
Posted: Thu Dec 17, 2009 9:38 pm Post subject: |
|
|
Hi, I am trying to use the {while} in a {foreach}, but I do not know how to make it work.
I am new to this.
Code: |
{* Front End: List of apex categories *}
<div class="catlistheader ">
{$label_categories}
</div>
<table>
<tr>
{assign var="counter" value="0"}
{while $counter <= 3}
{foreach from=$items item=entry}
{if $entry->image!='*none'}
<td> <div class="catimagethumb" [{$counter}] >
{image src=$entry->image alt=$entry->description} {$entry->name}
</div>
{else}
<div class="catlistline">
{$entry->name}
</div></td>
{/if}
{/foreach}
{ math equation="x + 1" x=$counter assign="counter" }
</td></tr>
{/while}
</table>
|
Maybe I am going about this the wrong way, I am not sure. Here's what I'm trying to do.
The {foreach} displays a number of items from the sql database, right now it shows a list of years, 1942 ... 2010
It displays like such in <dig> tags
1942
1943
1944
1945
...etc
However, I would like it to be displayed in columns of 3 then make a break in the table.
What am I doing wrong?
Thanks in advance. |
|
Back to top |
|
|