View previous topic :: View next topic |
Author |
Message |
4485670 Smarty n00b
Joined: 27 Apr 2009 Posts: 2
|
Posted: Mon Apr 27, 2009 3:26 pm Post subject: break and continue for looping functions |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Apr 27, 2009 4:24 pm Post subject: |
|
|
Code: | {foreach from=foo item=bar}
{if !$var}
<foo>{$bar}</foo>
{/if}
{/foreach} |
same diff. |
|
Back to top |
|
jLix Smarty Regular
Joined: 01 Apr 2009 Posts: 59 Location: Lowlands, EU
|
Posted: Mon Apr 27, 2009 4:30 pm Post subject: |
|
|
{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 |
|
Janis Smarty Rookie
Joined: 11 Jan 2007 Posts: 10
|
Posted: Wed Sep 16, 2009 11:41 am Post subject: |
|
|
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". |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Sep 17, 2009 12:01 am Post subject: |
|
|
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 |
|
Janis Smarty Rookie
Joined: 11 Jan 2007 Posts: 10
|
Posted: Thu Sep 17, 2009 11:02 pm Post subject: |
|
|
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. |
|
Back to top |
|
unexpectedkas Smarty n00b
Joined: 23 Feb 2010 Posts: 2
|
Posted: Wed Feb 24, 2010 11:22 am Post subject: |
|
|
I've found this solution:
Ferdinand Beyer wrote: |
Code: | <?php
// compiler.break.php
function smarty_compiler_break($contents, &$smarty)
{
return 'break;';
}
?>
<?php
//compiler.continue.php
function smarty_compiler_continue($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 |
|
SoN9ne Smarty Rookie
Joined: 25 Feb 2010 Posts: 17
|
Posted: Thu Mar 18, 2010 1:56 am Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Mar 18, 2010 5:12 pm Post subject: |
|
|
It must be
return '<?php break;?>';
and
return '<?php continue;?>'; |
|
Back to top |
|
Kim Steinhaug Smarty n00b
Joined: 26 Apr 2010 Posts: 3
|
Posted: Mon Apr 26, 2010 11:20 am Post subject: |
|
|
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 |
|
CaxtonQueens Smarty n00b
Joined: 25 Jul 2013 Posts: 1
|
Posted: Thu Jul 25, 2013 1:34 pm Post subject: |
|
|
{php}continue;{/php} will do the trick without additional plugins |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
|
Back to top |
|
|