logo       

Re: enhanced foreach loops: msg#00007

Subject: Re: enhanced foreach loops
Kerry Wilson wrote:
I would like to enhance the foreach loop by adding next and previous to the foreach variables.

Example usage:

{foreach from=$message.backtrace item='call' name='backtrace'}
 {assign var='previous_call' value=|$smarty.foreach.backtrace.next}|
<li style='list-style: none;'>{$call.file} [{$call.line}] - {$previous_call.class}.{$previous_call.function}(){/if}</li>
{/foreach}

In case you cannot tell I am using this to print a backtrace ( I want it to show the method it is currently in, not the one that it is calling ).

Where does the foreach implementation reside in source? And is this a good thing to add to the base code?


Here is the code for the feature enhancement, it goes in Smarty_Compiler.class.php around line 1185.

$output .= "if ({$foreach_props}['total'] > 0):\n";
$output .= "    \$_values = array_values(\$_from);\n";
$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n"; $output .= " if( isset( \$_values[{$foreach_props}['iteration']-1] ) ) { {$foreach_props}['prev'] = \$_values[{$foreach_props}['iteration']-1]; } else { {$foreach_props}['prev'] = false; }\n"; $output .= " if( isset( \$_values[{$foreach_props}['iteration']+1] ) ) { {$foreach_props}['next'] = \$_values[{$foreach_props}['iteration']+1]; } else { {$foreach_props}['next'] = false; }\n";
$output .= "        {$foreach_props}['iteration']++;\n";

It actually turned out to be cleaner than I expected. It would be great if this were added to the next release so my code doesn't break when I upgrade!

kw

--
Kerry Wilson
Lead Developer
Williams Web
kwilson@xxxxxxxxx | 423.485.4747


-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
audio.irate.dev...    yellowdog.gener...    ietf.ips/2002-0...    xfree86.fonts/2...    busybox/2003-07...    emacs.jdee/2004...    linux.mandrake....    hardware.microc...    user-groups.lin...    science.analysi...    version-control...    db.filemaker.de...    cluster.openmos...    mail.eyebrowse....    text.xml.xerces...    kde.devel.kwrit...    finance.moneyda...    gcc.regression/...    network.routing...    os.freebsd.deve...    recreation.radi...    qnx.openqnx.dev...    python.xml/2002...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe