KillGorack Posted February 12, 2022 Share Posted February 12, 2022 Hey, No real issue below works, but I'm trying to find best practices. Is the below normal, or is there a better more concise way to do it? private function lst($content) { $html = <<<"EOT" <table class="data clickable admin"> <thead> <tr> EOT; foreach(array_keys($content[0]) as $key){ $html .= <<<"EOT" <th>{$key}</th> EOT; } $html .= <<<"EOT" </tr> </thead> EOT; $html .= <<<"EOT" <tbody> EOT; foreach($content as $row){ $html .= <<<"EOT" <tr> EOT; foreach($row as $cell){ $html .= <<<"EOT" <td>{$cell}</td> EOT; } $html .= <<<"EOT" </tr> EOT; } $html .= <<<"EOT" </tbody> EOT; $html .= <<<"EOT" </table> EOT; return $html; } Quote Link to comment https://forums.phpfreaks.com/topic/314552-templating-and-loops-with-an-html-table/ Share on other sites More sharing options...
KillGorack Posted February 12, 2022 Author Share Posted February 12, 2022 Normally I would do this.. function tableInator($data, $class = "") { $html = "<table class=\"{$class}\">\r\n"; $html .= "<thead>\r\n"; $html .= "<tr><th>".implode("</th><th>", array_keys($data[0]))."</th></th>\r\n"; $html .= "</thead>\r\n"; $html .= "<tbody>\r\n"; foreach($data as $datum){ $html .= "<tr><td>".implode("</td><td>", $datum)."</td></tr>\r\n"; } $html .= "</tbody>\r\n"; $html .= "</table>\r\n"; return $html; } Quote Link to comment https://forums.phpfreaks.com/topic/314552-templating-and-loops-with-an-html-table/#findComment-1594097 Share on other sites More sharing options...
mac_gyver Posted February 12, 2022 Share Posted February 12, 2022 that's a LOT of typing, especially for the static sections containing no dynamic values. you need to use templates, with tags in it for the dynamic sections, then either write your own templating code or use one of the existing templating engines. here's the tag documentation for one of the popular templating engines showing how to display values, implement if conditional statements, and loops - https://github.com/bobthecow/mustache.php/wiki/Mustache-Tags Quote Link to comment https://forums.phpfreaks.com/topic/314552-templating-and-loops-with-an-html-table/#findComment-1594100 Share on other sites More sharing options...
Barand Posted February 12, 2022 Share Posted February 12, 2022 or go with an approach like this <?php // GET FUNCTION CALLS OUT OF THE WAY $thead = "<tr><th>".implode("</th><th>", array_keys($data[0]))."</th></th>\r\n"; $tdata = ''; foreach($data as $datum){ $tdata .= "<tr><td>".implode("</td><td>", $datum)."</td></tr>\r\n"; } // THEN $html = <<<HTML <table class="$class"> <thead> $thead </thead> <tbody> $tdata </tbody> </table> HTML; ?> Quote Link to comment https://forums.phpfreaks.com/topic/314552-templating-and-loops-with-an-html-table/#findComment-1594101 Share on other sites More sharing options...
mac_gyver Posted February 12, 2022 Share Posted February 12, 2022 template method - <?php require 'Mustache/src/Mustache/Autoloader.php'; Mustache_Autoloader::register(); $m = new Mustache_Engine; // define template $tpl = " <table{{#class}} class='{{class}}'{{/class}}> <thead> <tr> {{#headings}} <th>{{.}}</th> {{/headings}} </tr> </thead> <tbody> {{#rows}} <tr> {{#.}} <td>{{.}}</td> {{/.}} </tr> {{/rows}} </tbody> </table> "; // fake your existing data $class = "data clickable admin"; $data = []; $data[] = ['c1'=>'r0v1','c2'=>'r0v2','c3'=>'r0v3','c4'=>'r0v4','c5'=>'r0v5']; $data[] = ['c1'=>'r1v1','c2'=>'r1v2','c3'=>'r1v3','c4'=>'r1v4','c5'=>'r1v5']; $data[] = ['c1'=>'r2v1','c2'=>'r2v2','c3'=>'r2v3','c4'=>'r2v4','c5'=>'r2v5']; // array_map call-back function to get only array values function _array_values($arr) { return array_values($arr); } // populate template data $tpl_data = []; $tpl_data['class'] = $class; $tpl_data['headings'] = array_keys($data[0]); $tpl_data['rows'] = array_map('_array_values',$data); // echo the rendered template echo $m->render($tpl, $tpl_data); result - <table class='data clickable admin'> <thead> <tr> <th>c1</th> <th>c2</th> <th>c3</th> <th>c4</th> <th>c5</th> </tr> </thead> <tbody> <tr> <td>r0v1</td> <td>r0v2</td> <td>r0v3</td> <td>r0v4</td> <td>r0v5</td> </tr> <tr> <td>r1v1</td> <td>r1v2</td> <td>r1v3</td> <td>r1v4</td> <td>r1v5</td> </tr> <tr> <td>r2v1</td> <td>r2v2</td> <td>r2v3</td> <td>r2v4</td> <td>r2v5</td> </tr> </tbody> </table> note: htmlspecialchars is applied to all values by default, so any html special characters in a value won't break the html syntax, or allow html, css, or javascirpt to be operated on by the browser. Quote Link to comment https://forums.phpfreaks.com/topic/314552-templating-and-loops-with-an-html-table/#findComment-1594104 Share on other sites More sharing options...
maxxd Posted February 13, 2022 Share Posted February 13, 2022 I'm glad to know Mustache.php is still active - it's the first template system I used in php. Right now I'm using mostly Laravel so I'm on Blade, but before that I was using Twig, which is syntactically very similar and just kinda awesome. Quote Link to comment https://forums.phpfreaks.com/topic/314552-templating-and-loops-with-an-html-table/#findComment-1594105 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.