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

break and continue for looping functions

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


Joined: 27 Apr 2009
Posts: 2

PostPosted: Mon Apr 27, 2009 3:26 pm    Post subject: break and continue for looping functions Reply with quote

if not implemented yet can you implement it?

Code:

{foreach from=foo item=bar}
     {if $var === true}
        {continue}
     {/if}
     <foo>{$bar}</foo>
{/foreach}
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Apr 27, 2009 4:24 pm    Post subject: Reply with quote

Code:
{foreach from=foo item=bar}
     {if !$var}
       <foo>{$bar}</foo>
     {/if}
{/foreach}


same diff.
Back to top
View user's profile Send private message Visit poster's website
jLix
Smarty Regular


Joined: 01 Apr 2009
Posts: 59
Location: Lowlands, EU

PostPosted: Mon Apr 27, 2009 4:30 pm    Post subject: Reply with quote

{break} and {continue} would be nice indeed for both {foreach} and {section}, especially when combined width a name parameter:
Code:
{foreach from=$array item="foo" name="outer"}
    {foreach from=$foo item="bar" name="inner'}
        {if $bar == true}
            {break name="outer"}
        {/if}
        {$bar}
    {/foreach}
    {$foo.barcode}
{/foreach}

_________________
http://jlix.net/extensions/smarty
Back to top
View user's profile Send private message Visit poster's website
Janis
Smarty Rookie


Joined: 11 Jan 2007
Posts: 10

PostPosted: Wed Sep 16, 2009 11:41 am    Post subject: Reply with quote

I agree.

mohrt's solution would fit only such simple example jLix gave, it wouldn't do always. There is a reason why PHP has "continue". Wink
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Sep 17, 2009 12:01 am    Post subject: Reply with quote

The thing is, things like break and continue start adding more complexity to the templates. You should assign only what is needed for presentation, and simply loop over it. That is why Smarty 2 never had these features. If you start introducing ways to break loops, you pave a path for more business logic into the presentation layer. It is a fine line, is there a strong argument to put those features in?
Back to top
View user's profile Send private message Visit poster's website
Janis
Smarty Rookie


Joined: 11 Jan 2007
Posts: 10

PostPosted: Thu Sep 17, 2009 11:02 pm    Post subject: Reply with quote

You are right, of course.

In my particular case (why I sought out a thread like this), firstly, I cannot really change the data, I — as a template editor — must use it as it is.

Secondly, the code structure went like this:
Code:
{if <expression X>}
   {if <some complex expression Y>}
      Some output A
   {else}
      Some output B
   {/if}
{/if}

{if <NOT some complex expression Y>}
   Some output C
{/if}

And I wanted to "continue" just after "Some output A". It would be cleaner because 1) "some complex expression Y" was not too short and it would clatter up the template and 2) the same condition wouldn't have to be written in two places.

I ended up creating a flag variable after "Some output A" and using it later instead of the complex expression.

So, of course it is doable in other ways, it just sometimes gives a cleaner look. Smile
Back to top
View user's profile Send private message
unexpectedkas
Smarty n00b


Joined: 23 Feb 2010
Posts: 2

PostPosted: Wed Feb 24, 2010 11:22 am    Post subject: Reply with quote

I've found this solution:

Ferdinand Beyer wrote:

Code:
<?php
// compiler.break.php
function smarty_compiler_bre­ak($contents, &$smarty)
{
return 'break;';
}

?>
<?php
//compiler.continue­.php
function smarty_compiler_con­tinue($contents, &$smarty)
{
return 'continue;';
}

?>


Create these two files and put them into your plugins directory
(notice the naming convention compiler.xxx.php). Now, you should
be able to leave foreach and section loops with {break} and to leave
out one step with {continue}.


On: qaix com
Back to top
View user's profile Send private message
SoN9ne
Smarty Rookie


Joined: 25 Feb 2010
Posts: 17

PostPosted: Thu Mar 18, 2010 1:56 am    Post subject: Reply with quote

I have not been able to get that to work. Any luck on your side?

I really need the ability of continue so I have been looking for awhile now.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Mar 18, 2010 5:12 pm    Post subject: Reply with quote

It must be

return '<?php break;?>';

and

return '<?php continue;?>';
Back to top
View user's profile Send private message
Kim Steinhaug
Smarty n00b


Joined: 26 Apr 2010
Posts: 3

PostPosted: Mon Apr 26, 2010 11:20 am    Post subject: Reply with quote

This works prefectly. Just note that when copying the above snippets there are some spaces inserted which you must remove manually.

To sum it up:
In your libs/plugins folder create 2 new files:

1. compiler.break.php
Code:
<?php
function smarty_compiler_break($contents, &$smarty){
  return 'break;';
}
?>


2. compiler.continue.php
Code:
<?php
function smarty_compiler_continue($contents, &$smarty){
  return 'continue;';
}
?>


Then in your template the following code will work:
Code:
{foreach from=$array item=entry name=loop}
  {if $entry.variable=='something'}
    {continue}
  {/if}
{/foreach}


regards,
Kim Steinhaug
http://www.steinhaug.no/ http://www.easywebshop.no/ http://www.easycms.no/
Back to top
View user's profile Send private message
CaxtonQueens
Smarty n00b


Joined: 25 Jul 2013
Posts: 1

PostPosted: Thu Jul 25, 2013 1:34 pm    Post subject: Reply with quote

{php}continue;{/php} will do the trick without additional plugins
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Jul 25, 2013 3:05 pm    Post subject: Reply with quote

Smarty 3.1 does support {break} and {continue} tags.

See http://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.construct.break

It does work within in {for}, {foreach}, {section} and {while} loops.
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 -> Smarty 3 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