This article mainly introduces PHP custom function implementation assign () array assigned to template and extract () variable assignment to template function, can simulate the template variable allocation function in TP frame, involving PHP based on object-oriented array assignment related operation skills, need friends can refer to the next
This example describes the PHP custom function implementation assign () array assigned to templates and extract () variables assigned to the template function. Share to everyone for your reference, as follows:
This simulates the TP frame template variable assignment and assignment operation.
extract($arr);
Extract function: Import variables from the array into the current symbol table, the key to do the variable, value to do value!
compact();
-Create an array, including the variable names and their values
Class base{public $array; public $key; public $val; Public function assign ($key, $val) { if (array ($val)) { $this->array["$key"] = $val; } else{ $this->array["$key"] = compact ($val); } } Public function display ($TPL) { $this->assign ($this->key, $this->val); Extract ($this->array); if (file_exists ($TPL)) {//template exists on the load file. include $tpl;}}} Class Indexcontroller extends base{public function Index () { $arr = array (' a ' = ' = ' aaaaaaa ', ' B ' =>array (' A ' = ' 111111 ', ' b ' = ' 22222 ', ' c ' = ' 3333 '), ' c ' = ' CCCCCCC ', ' d ' and ' dddddd ', ' e ' = ' eeeee '); $str = ' I am a string '; $this->assign (' arr ', $arr); $this->assign (' str ', $str); $this->display (' index.html ');} } $base = new Base; $base->index ();