<?
/*
* Hello World 4
*
* Sometimes (or even more often), we won't be inserting just a string into content. It would
* be a complicated structure of different HTML objects. In this case we will have to equip
* our class with it's own template. Class is going to manipulate template and produce nicely
* formatted output
*
* It does not matter in which file and under which tag template is defined, we just specify
* how the tag can be found. In our case, i have created a file called widgets.html. It would be
* a file containing few different templates for few different classes. So this file won't be
* the only one to use that template.
*
*/
include '../../trunk/loader.php';
class HelloWorld extends AbstractView {
/*
* Now our class produces more complex output and needs it's own template. Template are assigned
* to our class when we call $api->add('HelloWorld') by passing 4th argument to it.
*
*/
public $data;
function init(){
$this->data = array(
array('name'=>'Janis','surname'=>'Lielmanis'),
array('name'=>'Juris','surname'=>'Pimanovs'),
array('name'=>'Aleksejs','surname'=>'Chizhevskis'),
array('name'=>'Viesturs','surname'=>'Zarinsh'),
);
}
function render(){
$r=$this->template->cloneRegion('row');
/* inside our template there is a region called row. By default it's initialized with one
* row which we will use as a template. So we copy it into different template and remove
* the one from parent
*/
$this->template->del('rows');
/*
* We will be outputing names of the people we want to send hello to.
*/
foreach($this->data as $row){
$r->set($row);
/*
* SMlite allows us to initialize template from associative array. We should make sure
* name/surname keys in $row spelled just like in <?$name?> and <?$surname?>. If those
* won't match they will be ignored.
*/
$this->template->append('rows',$r->render());
/*
* Now when we have inserted all values into our row template we are rendering it.
* Resulting HTML code (a string) is appended to main template of our object
*/
}
$this->template->set('title','Hello to ..');
/*
* our template is properly formated now, we can call parent's render method, which
* will render and output this->template
*/
return parent::render();
}
}
$api = new ApiWeb('hw4');
$api->template = $api->add('SMlite')->loadTemplate('hw'); // using file templates/hw.html
$api->template->set('page_title','Hello world application');
//$api->template->set('page_content','Dynamic Hello world ('.rand().')');
$api->add('HelloWorld',null,'page_content',array('widgets','hw4'));
/*
* Notice that we have to specify another argument. If we would specify it as:
* 'hw4'
* then library will search for this tag inside our api's template. Since we have
* a separate file for that, we specify it's name:
* array('widgets','hw4');
*
* There are other ways to specify 4th argument (for example to use already
* initialized SMlite object)
*/
$api->main();