My array "$groupData" looks like this:
array(3) (
0 => array(3) (
"id" => string(1) "2"
"name" => string(20) "Super Administrators"
"permissions" => array(3) (
"system" => integer 1
"superuser" => string(1) "1"
"admin" => string(1) "1"
)
)
1 => array(3) (
"id" => string(1) "3"
"name" => string(10) "Publishers"
"permissions" => array(4) (
"system.pub.add" => integer 1
"system.pub.delete" => integer 1
"system.pub.edit" => integer 1
"system.pub.publish" => integer 1
)
)
2 => array(3) (
"id" => string(1) "4"
"name" => string(7) "Authors"
"permissions" => array(3) (
"system.pub.add" => integer 1
"system.pub.delete" => integer 1
"system.pub.edit" => integer 1
)
)
)
I am having trouble rendering the permissions
part of my code. I have tried using {{#groupData}}{{#permissions}} {{.}} {{/permissions}}{{/groupData}}
but it doesn't work.
Your problem is that {{ . }}
... You're trying to render an associative array as a string, and that just doesn't work in PHP:
<?php
echo array('foo' => 1, 'bar' => 2); // no me gusta :)
Like most things Mustache, the answer here is to prepare your view. What exactly would you expect it to render that array as? JSON? If so, make it explicit:
<?php
foreach ($data['groupData'] as $key => $group) {
$data['groupData'][$key]['permissionsJSON'] = json_encode($group['permissions']);
}
$tpl = "{{# groupData }}{{ permissionsJSON }}{{/ groupData }}";
$m = new Mustache_Engine;
echo $m->render($tpl, $data);
If you want it available as pairs of key/value, do that explicitly:
<?php
foreach ($data['groupData'] as $key => $group) {
$permissions = array();
foreach ($group['permissions'] as $key => $val) {
$permissions[] = array('key' => $key, 'val' => $val);
}
$data['groupData'][$key]['permissions'] = $permissions;
}
$tpl = "{{# groupData }}{{# permissions }}{{ key }}: {{ val }}{{/ permissions }}{{/ groupData }}";
$m = new Mustache_Engine;
echo $m->render($tpl, $data);
Do you just want to iterate over a list of the keys?
<?php
foreach ($data['groupData'] as $key => $group) {
$data['groupData'][$key]['permissions'] = array_keys($group['permissions']);
}
$tpl = "{{# groupData }}{{# permissions }}{{ . }}{{/ permissions }}{{/ groupData }}";
$m = new Mustache_Engine;
echo $m->render($tpl, $data);