-
Notifications
You must be signed in to change notification settings - Fork 28
Loop thru multi dimensional array
#Looping through a multi-dimensional array
This is a simple example to show how to loop thru multi-dimensional arrays (like a table of many rows and columns).
Haanga allows you to create a template file that contains special tags, like {{ variable_name }}.
Then your php code calls the Haanga class to replace those tags with values that you set in your script.
It then creates a file that only contains php code, like <?php echo variable_name; ?>
This gives you two benefits:
* Web Designers can create templates without needing to know php code, and without the security risk of including php code in a template.
* The template file is compiled into pure php code and stored in a temporary folder, so it runs as fast as regular php code.
/haangatest.php *the php script (shown below)
/lib/Haanga.php *the Haanga class file in folder named lib (library of code files)
/lib/Haanga *a folder containing functions for the Haanga template engine
/hgtmpl *a folder containing templates to be used by Haanga
/hgtmpl/haanga_test.html *a template file (shown below)
/hgtemp *a folder containing the compliled templates that use php code to echo the php variable values
##php code (/haangatest.php) <?php // haanga test example
require(__DIR__."/lib/Haanga.php");
// set Haanga configuration values
$config = array
(
'cache_dir' => 'hgtemp/',
'template_dir' => 'hgtmpl/',
);
// set the config values into the class through a static method
Haanga::Configure($config);
// create the multi-dimensional array
$rows = 1000;
$data = array();
for($i = 0; $i < $rows; $i++)
{
$data[] = array('id' => $i, 'name' => "name {$i}");
}
/* set the value of variables to be passed to the template.
it is an array, because there are many variables to pass to the template
*/
$vars = array
(
'number'=>$rows, // a variable named 'number' with value is the php variable $rows, set above
'title'=>'haanga', // a variable named 'title' with value of 'haanga'
'table'=> $data, // a variable named 'table' with value of the array named 'data'
);
// load template and send variables to it
Haanga::Load('haanga_test.html', $vars );
##template code (/hgtmpl/haanga_test.html) <!-- template file title is a variable number is a variable table is a variable which equals the multidimensional array $data
for row in table,
means it will loop thru the multi-dimensional array represented by the variable "table"
this array is made of up key/values,
the key is just a number that represents the position of the element in the array (0,1,2, etc)
the value is a nested array,
the nested array has 2 elements, id and name
so for each row in the array named table
call the row,
and get the value of the key named 'id'
and get the value of the key named 'name'
-->
<h1>{{title}}</h1>
<p>Table with {{number}} rows.</p>
<table border="1">
<tr>
<td><b>ID</b></td>
<td><b>NAME</b></td>
</tr>
{% for row in table %}
<tr>
<td>{{row.id}}</td>
<td>{{row.name}}</td>
</tr>
{% endfor %}
</table>