phparraysmustachemustache.php

looping through two dimensional array and passing to mustache template


so I am just getting started using mustache.php and I am stuck trying to loop though a two dimensional array. I have an array that looks like this...

 $FeedArray = array(3) { [0]=> array(3) { ["entity"]=> string(10) "mail" 
                                          ["time"]=> string(19) "2015-02-05 05:10:26"
                                          ["title"]=> string(0) "what's up?" }         
                         [1]=> array(3) { ["entity"]=> string(5) "event" 
                                          ["time"]=> string(19) "2015-02-05 03:16:54"
                                          ["title"]=> string(15) "asfasggasdgasdg"  } 
                         [2]=> array(3) { ["entity"]=> string(10) "mail"
                                          ["time"]=> string(19) "2015-01-11 14:24:08"
                                          ["title"]=> string(24) "lunch?" } }

I am trying to loop though it like this:

$eventTemplate = file_get_contents('templates/GroupPageEvent.mustache');
$postTemplate = file_get_contents('templates/GroupPagePost.mustache');

       foreach ($FeedArray as $entity => $row){

              if ($row['entity_type']=='mail'){
                     echo $m->render($postTemplate, $entity);
              }

              if ($row['entity_type']=='event'){
                     echo $m->render($eventTemplate, $entity); 
              }

       }

I know my templates are working well and all. Just am not passing in the subarray($entity) properly, and all the outputted templates are empty.

The if $row['entity_type'}==? is reading properly as well.

Any Help appreciated.


Solution

  • It's because you pass key to your render function, $entity contains array keys (0,1,2...) and your enity array is stored in $row

    foreach ($FeedArray as $entity => $row){
    

    in that case you should do this :

    echo $m->render($postTemplate, $row);
    

    and also in array you got 'entity' key not 'entity_type' so change this too:

    $row['entity_type']=='mail'
    

    to:

    $row['entity']=='mail'