Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Templates -> Loops -> foreach: question

edited July 2013 in Documentation

I would like to looping a non-associative two dimensional array so I cannot use index - value (key -value) syntax.

How can I do that?

Thanks.

Comments

  • Here are examples of a few ways to do loops in templates:

    https://gist.github.com/jbroadway/6070592

    Hope that helps!

  • Sorry I was not clear (and ungrammatical).

    In the handler I import a text file into an array that should look as follows:

    
    $files = array (
            array(
                    'img' => '/files/customslide/01.JPG',
                    'hrf' => '/contact',
                    'tgt' => '_self',
                    'tit' => 'Slide 1',
                    'txt' => 'Lorem ipsum 1 dolor sit amet, consectetur adipiscing elit. Proin vel pellentesque risus, eget euismod eros. Curabitur vulputate ipsum at nibh lobortis, vel pharetra neque laoreet.',
            ),
            array(
                    'img' => '/files/customslide/02.JPG',
                    'hrf' => 'http://www.google.it',
                    'tgt' => '_self',
                    'tit' => 'Slide 2',
                    'txt' => 'Lorem ipsum 1 dolor sit amet, consectetur adipiscing elit. Proin vel pellentesque risus, eget euismod eros. Curabitur vulputate ipsum at nibh lobortis, vel pharetra neque laoreet.',
            ),
            array(
                    'img' => '/files/customslide/04.jpg',
                    'hrf' => 'http://www.google.com',
                    'tgt' => '_self',
                    'tit' => 'Slide 3',
                    'txt' => 'Lorem ipsum 1 dolor sit amet, consectetur adipiscing elit. Proin vel pellentesque risus, eget euismod eros. Curabitur vulputate ipsum at nibh lobortis, vel pharetra neque laoreet.',
            ),
            array(
                    'img' => '/files/customslide/05.jpg',
                    'hrf' => 'http://www.google.fr',
                    'tgt' => '_self',
                    'tit' => 'Slide 4',
                    'txt' => 'Lorem ipsum 1 dolor sit amet, consectetur adipiscing elit. Proin vel pellentesque risus, eget euismod eros. Curabitur vulputate ipsum at nibh lobortis, vel pharetra neque laoreet.',
            )
    );
    
    

    So I render the template with:

    
    ...
    echo $tpl->render ('custom/slideshow', array ('files' => $files));
    
    

    In the template I have:

    
    ...
    {% foreach files %}
        <a href="{{ loop_value->hrf }}" target="{{ loop_value->tgt }}"><img src="{{ loop_value->img }}" style="" /></a>
    {% end %}
    ...
    
    

    But it does not work.

    (Keys "tit" and "txt" are not used in the example.)

    Thanks for any help.

  • Ah I see now. Try this:

    {{ loop_value[img] }}
    

    It doesn't auto convert sub-arrays to objects so I think you just need to use the array syntax.

  • edited July 2013

    Oh yes! That works. Thanks.

    I was going to write that I found a solution when I saw yours (I have not received the email notification).

    I had solved converting the array to an object with:

    $filesobj = json_decode(json_encode($files), FALSE);
    

    and using {{ loop_value->key }}

    Inside the curly brackets, can I use all the PHP I want?

Sign In or Register to comment.