ALPHA Framework: Model 2.x … what’s new?

Iterator enhancements

Version 2.2 and version 2.3 are instead focused on iteration enhancements. Two are the news concerning the loop.

The first is the introduction of  {var:iterator.count} variable available inside the {foreach} {loop} construct. This new variable tells you the number of elements affected by iteration.

The second new is the introduction of the keyword {break}. This special command tells you to break a specific loop.

The following example describes better how it works.

Let’s imagine we have exposed to the template an array of contacts structured as:

contact = [
    { 'name': 'Diego La Monica', 'phone': '+39 1.. ' },
    { 'name': 'Fabrizio Caccavello', 'phone': '+39 2.. '},
    { 'name': 'Federico Volpini', 'phone': '+39 3..'}
    // And so on!
];
<!-- 
the following code loops through the contact list you have exposed
to the template and exit from the loop if the number of contacts reach
the maximum number
-->
<ul id="contact-list">
{foreach:contact}
    <li>{var:contact.name} Phone Number: {var:contact.phone}</li>
    {if:$this->getVar('iterator') == $this->getVar('maxContactsToDisplay') }
        {break:contact}
    {endif}
{loop}
</ul>

You can use break to exit from a parent (or any outer) loop. The following example clarify better what I mean.

This is what I have exposed to the template:

contact = [
    { 'letter': 'D',
      'list' : [{ 'name': 'Diego La Monica', 'phone': '+39 1.. ' }]
    },
    { 'letter': 'F',
      'list': [{ 'name': 'Fabrizio Caccavello', 'phone': '+39 2.. '},
              { 'name': 'Federico Volpini', 'phone': '+39 3..', 'break': true},
              { 'name': 'Fake User never displayed', 'phone' : '+39 999...'}]
    },
    { 'letter': 'N',
      'list': [{'name': 'Never displayed contact', 'phone' : '+39 111...'}]
    }
     // And so on!
];

And this is the template I have used:

<!-- 
the following code loops through the contact list you have exposed
to the template and exit from the loop if the number of contacts reach
the maximum number or if the variable contact.list.break exists and is true.
-->
<ul id="contact-list">
    {foreach:contact}
        <li>
            <h2>Letter {contact.letter}</h2>
            <ul>
                {foreach:contact.list}
                    <li>
                        {var:contact.list.name} 
                        Phone Number: {var:contact.list.phone}
                    </li>
                    {if:$this->getVar('iterator') == $this->getVar('maxContactsPerGroup')}
                        <!-- 
                        This forces the exit from the current loop "contact.list"
                        -->
                        {break:contact.list}
                    {endif}
                    {if:$this->getVar('contact.list.break', false)}
                       <!-- 
                       This forces the exit from the main loop "contact"
                       -->
                       {break:contact}
                    {endif}
                {loop}
            </ul>
        </li>
    {loop}
</ul>

All changes we made to ALPHA Framework are focused to the simplification of making Web Applications! So you can understand that Coding Never Been So Smart!

Note: If you’re fascinated or just curious about this framework you can follow updates following ALPHA Framework on Twitter and watch the official repository or just tell us you like ALPHA Framework on Facebook!


Pubblicato

in

da

Commenti

Una risposta a “ALPHA Framework: Model 2.x … what’s new?”

  1. […] the latest post on this blog, I was writing about news regarding Class Model of ALPHA […]

%d blogger hanno fatto clic su Mi Piace per questo: