Example of PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function

jacklove
Release: 2023-04-01 18:02:01
Original
1934 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

You may be interested Article:

PHP imitates tp to implement the basic design ideas and implementation methods of the mvc framework analysis

yii2 installation detailed process_php example

Database addition, deletion, modification and query operations implemented by CI framework (CodeIgniter)

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

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