1

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.

cracker
  • 4,900
  • 3
  • 23
  • 41
ambe5960
  • 1,870
  • 2
  • 19
  • 47
  • You defined `entry` but reading `entry_type`. Change `$row['entity_type']` to `$row['entity']` – Bart Haalstra Feb 06 '15 at 08:38
  • Unrelated: you'll get a lot better performance if you move the template parsing outside of your `foreach` loop. Change the first two rows to something like this: `$eventTpl = $m->loadTemplate(file_get_contents(...))` and change the call inside the loop to `echo $eventTpl->render($entity)`. – bobthecow Feb 07 '15 at 05:48

1 Answers1

1

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'
szapio
  • 998
  • 1
  • 9
  • 15