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

while-loop plugin

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
giksos
Smarty n00b


Joined: 23 Mar 2006
Posts: 3

PostPosted: Sat Mar 25, 2006 3:52 am    Post subject: while-loop plugin Reply with quote

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 Wink
P.P.S. Improvement suggestion for the forum: print code in fixed-width font!
Back to top
View user's profile Send private message
lordlamer
Smarty n00b


Joined: 06 Sep 2006
Posts: 4

PostPosted: Wed Sep 06, 2006 9:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
giksos
Smarty n00b


Joined: 23 Mar 2006
Posts: 3

PostPosted: Wed Sep 06, 2006 11:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Sep 07, 2006 1:16 am    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


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

PostPosted: Thu Sep 07, 2006 9:49 am    Post subject: Reply with quote

giksos wrote:
You should ask the authors of smarty whether the native while ever gets implemented.


I doubt it will.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
lordlamer
Smarty n00b


Joined: 06 Sep 2006
Posts: 4

PostPosted: Thu Sep 07, 2006 9:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
lordlamer
Smarty n00b


Joined: 06 Sep 2006
Posts: 4

PostPosted: Thu Sep 07, 2006 9:23 pm    Post subject: Reply with quote

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 Wink

regards,
Frank
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Sep 07, 2006 11:19 pm    Post subject: Reply with quote

@lorlamer: Not too difficult once you know the technique for it (or should I say idiosyncratic behaviour of it Smile )

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
View user's profile Send private message
lordlamer
Smarty n00b


Joined: 06 Sep 2006
Posts: 4

PostPosted: Fri Sep 08, 2006 9:38 am    Post subject: Reply with quote

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
View user's profile Send private message
ITF
Smarty n00b


Joined: 17 Dec 2009
Posts: 1

PostPosted: Thu Dec 17, 2009 9:38 pm    Post subject: Reply with quote

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}&nbsp;{$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
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 -> Plugins 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