PHP custom function implements assign() array assignment to template and extract() variable assignment to template

不言
Release: 2023-03-28 22:12:02
Original
1599 people have browsed it

This article mainly introduces the PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function. It can simulate the template variable assignment function in the tp framework and involves PHP object-oriented array assignment. For operational skills, friends in need can refer to

This article describes the example of PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function. Share it with everyone for your reference, the details are as follows:

Here simulates the tp framework template variable allocation and assignment operations.

extract($arr); //The role of extract: import variables from the array into the current symbol table, the keys are used as variables, and the values ​​are used as values!
compact(); // — Create an array, including 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)){ //模板存在就加载文件。
      include $tpl;
    }
  }
}
class indexcontroller extends base{
  public function index(){
    $arr = array('a'=>'aaaaaaa','b'=>array('a'=>'111111','b'=>'22222','c'=>'3333'),'c'=>'ccccccc','d'=>'dddddd','e'=>'eeeee');
    $str = '我是字符串';
    $this->assign('arr',$arr);
    $this->assign('str',$str);
    $this->display('index.html');
  }
}
$base = new base;
$base->index();
Copy after login

Related recommendations:

php strftime function to obtain date and time (switch usage)

The above is the detailed content of PHP custom function implements assign() array assignment to template and extract() variable assignment to template. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!