Exemple d'utilisation de la fonction bab printTemplate()
Un article de OviWiki.
Exemple de code php et template pour réaliser une page HTML
| Template | Php |
|
Contenu du fichier test.html :
<!--#begin mytemplate -->
<!--#if count "> 0" -->
<ul>
<!--#in getfile -->
<li>{ filename }</li>
<!--#endin getfile -->
</ul>
<!--#else count -->
There are no files !
<!--#endif count -->
<!--#end mytemplate -->
|
class myClass { private $tab = array(); public $filename; public $count; public function __construct() { // fill $tab with file names $this->tab[] = "file1.txt"; $this->tab[] = "file2.doc"; $this->tab[] = "file1.gif"; $this->tab[] = "file1.html"; $this->count = sizeof($this->tab); } public function getfile() { static $i = 0; if( $i < this->count) { $this->filename = $this->tab[$i]; $i++; return true; } else return false; } } // instantiate class and call babPrintTemplate function $cl = new myClass(); |
|
bab_printTemplate( $cl, "test.html", "mytemplate"); | |
Résultat obtenu (retour de bab_printTemplate())
<ul>
<li>file1.txt</li>
<li>file2.doc</li>
<li>file1.gif</li>
<li>file1.html</li>
</ul>
