ED php framework :: Tutorial #10
Data repeater
You use TDataRepeater to visualize the data stored in a data source(see the previous tutorial). In this tutorial I will show how is possible to create a data grid using data source and data repeater.


You need to define in a file 6 sections - HEADER, REPEAT, REPEAT-ALTERNATE, REPEAT-EMPTY, REPEAT-SEPARATOR AND FOOTER.
snippet/data_repeater/
persons.tpl
[header]
<table>
  <tr style="color:blue">
     <td colspan="6">{#caption}</td>
  </tr>
  <tr style="color:blue">
     <td>PHOTO</td>
     <td>PERSON ID</td>
     <td>FIRST NAME</td>
     <td>LAST NAME</td>
     <td>COUNTRY</td>
     <td>BIRTHDAY</td>
  </tr>
 
 
[repeat]
  <tr style="color:white">
     <td>{#photo}</td>
     <td>{#person_id}</td>
     <td>{#first_name}</td>
     <td>{#last_name}</td>
     <td>{#country}</td>
     <td>{#birthday}</td>
  </tr>
 
 
 
[repeat-alternate]
  <tr style="color:grey">
     <td>{#photo}</td>
     <td>{#person_id}</td>
     <td>{#first_name}</td>
     <td>{#last_name}</td>
     <td>{#country}</td>
     <td>{#birthday}</td>
  </tr>
 
 
[repeat-empty]
  <tr style="color:white">
     <td colspan="6">no data</td>
  </tr>
 
 
 
[repeat-separator]
  <tr style="color:dark-grey;height:1px">
     <td colspan="6"></td>
  </tr>
 
 
 
[footer]
</table>


Then you bind a data source to it like this:
    // from file
    $dataSource = TDataSource::load("persons.ds.xml");
    // from database
    $dataSource = $db->select("select * from persons");

    $dataRepeater = new TDataRepeater();
    $dataRepeater->loadTemplateFromFile("persons.tpl");
    $dataRepeater->setDataSource($dataSource);

         // Optional. Rarely used
         $dataRepeater->header->replace("{#caption}", "LIST OF PERSONS"); // HEADER
         $dataRepeater->footer->replace("{#xxx}", "Some string value"); // FOOTER
         $dataRepeater->repeatA->replace("{#xxx}", "Some string value"); // REPEAT-ALTERNATE
         $dataRepeater->repeatE->replace("{#xxx}", "Some string value"); // REPEAT-EMPTY

    $dataRepeater->build(3);
    $html = $dataRepeater->getContent();


Take a look at this line:
    $dataRepeater->build(3);
It means that we want our grid always to show 3 rows. And if the data source has only 2 rows then the rest will be filled with REPEAT-EMPTY. If you use this method without argument like
    $dataRepeater->build();
then REPEAT-EMPTY will never be used.

Your "persons.tpl" must be located under directory "protected/tpl".