Array Helper

In many cases, a template for frontend output is built from HTML and “echo” of the PHP variables from the output array.

With a debug output as described in Debug Templates, you can see the array available in the template with its nodes as a tree structure.

Transferring this into the template can become quite tedious, especially when there are many relations to other MetaModels.

With the following “Array Helper”, the array nodes are output in a way that allows easy copy & paste into the template.

The respective template is extended with the following lines at the top:

 1 <?php
 2 // http://stackoverflow.com/a/14518402
 3 function printArray($array, $path=false, $top=true) {
 4     $data = ""; $delimiter = "~~|~~"; $p = null;
 5     if(is_array($array)){
 6         foreach($array as $key => $a){
 7             if(!is_array($a) || empty($a)){
 8                 if(is_array($a)){
 9                     $data .= $path."['{$key}'] = array();".$delimiter;
10                 } else {
11                     $data .= $path."['{$key}'] = \"".addslashes($a)."\";".$delimiter;
12                 }
13             } else {
14                 $data .= printArray($a, $path."['{$key}']", false);
15             }
16         }
17     }
18     if($top){
19         $return = "";
20         foreach(explode($delimiter, $data) as $value){
21             if(!empty($value)){ $return .= '$arrItem'.$value."\n"; }
22         };
23
24         return $return;
25     }
26
27     return $data;
28 }
29
30 echo "<!-- DEBUG START\n";
31 echo "<pre>\n";
32 // 0th node only
33 //print_r($this->items->parseAll($this->getFormat(), $this->view)[0]);
34 echo printArray($this->items->parseAll($this->getFormat(), $this->view)[0]);
35 echo "</pre>\n";
36 echo "DEBUG END -->\n";
37 ?>

The template should then begin with the following lines — for a better view, switch to source code view (Ctrl + U | Cmd + Alt + U):

 1<html>
 2 <!-- DEBUG START
 3 <pre>
 4 $arrItem['raw']['id'] = "93";
 5 $arrItem['raw']['pid'] = "0";
 6 $arrItem['raw']['sorting'] = "0";
 7 $arrItem['raw']['tstamp'] = "1484897086";
 8 $arrItem['raw']['name'] = "0";
 9 $arrItem['raw']['firstname'] = "Amir";
10 $arrItem['raw']['email'] = "Amir.Avery@mmtest.com";
11 $arrItem['raw']['department']['__SELECT_RAW__']['id'] = "4";
12 $arrItem['raw']['department']['__SELECT_RAW__']['pid'] = "0";
13 $arrItem['raw']['department']['__SELECT_RAW__']['sorting'] = "0";
14 $arrItem['raw']['department']['__SELECT_RAW__']['tstamp'] = "1442499032";
15 $arrItem['raw']['department']['__SELECT_RAW__']['name'] = "Marketing";
16 $arrItem['raw']['department']['__SELECT_RAW__']['alias'] = "marketing";
17 $arrItem['raw']['department']['name'] = "Marketing";
18 $arrItem['raw']['department']['alias'] = "marketing";
19 $arrItem['text']['name'] = "Avery";
20 $arrItem['text']['firstname'] = "Amir";
21 $arrItem['text']['email'] = "Amir.Avery@mmtest.com";
22 $arrItem['text']['department'] = "Marketing";
23 $arrItem['attributes']['name'] = "Last name";
24 $arrItem['attributes']['firstname'] = "First name";
25 $arrItem['attributes']['email'] = "E-Mail";
26 $arrItem['attributes']['department'] = "Department";
27 $arrItem['html5']['name'] = "<span class=\"text\">0</span>";
28 $arrItem['html5']['firstname'] = "<span class=\"text\">Amir</span>";
29 $arrItem['html5']['email'] = "<span class=\"text\">Amir.Avery@mmtest.com</span>";
30 $arrItem['html5']['department'] = "Marketing";
31 $arrItem['class'] = "first even";
32 $arrItem['jumpTo'] = array();
33 </pre>
34 DEBUG END -->
35 ...
36</html>

In the template, the output of the department could then look like this:

1<html>
2...
3<p><span class="label"><?= $arrItem['attributes']['department'] ?>:</span> <?= $arrItem['raw']['department']['name'] ?></p>
4...
5</html>

The output can be removed by commenting out the output block, deleting it, or switching to a different template.