Jump to content

templating, and loops with an HTML table


KillGorack

Recommended Posts

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;
    }

 

Link to comment
Share on other sites

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;
  }

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.