Home  >  Article  >  Backend Development  >  PHP framework - PHP tried to write its own template engine. The final display method is actually to require an HTML file. Why is it output?

PHP framework - PHP tried to write its own template engine. The final display method is actually to require an HTML file. Why is it output?

WBOY
WBOYOriginal
2016-08-30 09:36:461120browse

Will the php require function output when it encounters text?
This is the template class I wrote:
<?php

/**
 * User: 火蜥蜴制作
 */

namespace Core;

// 模板类
class Template
{
    private $data = [];
    private $path = ''; // 模板路径

    public function __construct() {
        $this->path = Config::get('project.template_path');
    }

    /**
     * 模板赋值
     * @param $key
     * @param $value
     */
    public function assign($key, $value) {
        if(is_array($key)) {
            $this->data = array_merge($this->data, $key);
        } else {
            $this->data[$key] = $value;
        }
    }

    /**
     * 获取路径
     * @param $file
     */
    private function getFilePath($file) {
        $params = explode('.', $file);
        // 模板路径已经加了分隔符
        $path = ROOT . DIRECTORY_SEPARATOR . $this->path;
        foreach ($params as $key => $param) {
            if($key == count($params) - 1) {
                $path .= $param;
            } else {
                $path .= $param . DIRECTORY_SEPARATOR;
            }
        }
        return $path . '.html';
    }


    public function display($file) {
        if(empty($file)) {
            throw new \Exception("Template Can Not Be Empty");
        }
        $realPath = $this->getFilePath($file);
        if(is_file($realPath)) {
            extract($this->data);
            require($realPath);
        } else {
            throw new \Exception("Template:<code>{$realPath} Not Found");
        }
    }
}

                            
                        

Reply content:

Will the php require function output when it encounters text?
This is the template class I wrote:
<?php

/**
 * User: 火蜥蜴制作
 */

namespace Core;

// 模板类
class Template
{
    private $data = [];
    private $path = ''; // 模板路径

    public function __construct() {
        $this->path = Config::get('project.template_path');
    }

    /**
     * 模板赋值
     * @param $key
     * @param $value
     */
    public function assign($key, $value) {
        if(is_array($key)) {
            $this->data = array_merge($this->data, $key);
        } else {
            $this->data[$key] = $value;
        }
    }

    /**
     * 获取路径
     * @param $file
     */
    private function getFilePath($file) {
        $params = explode('.', $file);
        // 模板路径已经加了分隔符
        $path = ROOT . DIRECTORY_SEPARATOR . $this->path;
        foreach ($params as $key => $param) {
            if($key == count($params) - 1) {
                $path .= $param;
            } else {
                $path .= $param . DIRECTORY_SEPARATOR;
            }
        }
        return $path . '.html';
    }


    public function display($file) {
        if(empty($file)) {
            throw new \Exception("Template Can Not Be Empty");
        }
        $realPath = $this->getFilePath($file);
        if(is_file($realPath)) {
            extract($this->data);
            require($realPath);
        } else {
            throw new \Exception("Template:<code>{$realPath} Not Found");
        }
    }
}

                            
                        
            

  1. require is not a function

  2. The function of
  3. require is to use the following string as the file name. Regardless of whether the file extension is .php, the file is considered to be a php program and is introduced into the current program to run.

  4. If the
  5. php program is not wrapped by <?php and ?>, it will be output directly.

require will execute the referenced file as a PHP file, no matter what suffix it is (no suffix is ​​acceptable), some PHP Trojans use this to bypass the firewall.
PHP code needs to be placed between <?php and ?> to be executed.

Crab Demon.

When a file is included, the parser leaves PHP mode and enters HTML mode at the beginning of the target file, and resumes at the end of the file. For this reason, any code in an object file that needs to be executed as PHP code must be included within valid PHP start and end tags.

When

include or requrie a file, if there are no php start tags and end tags in the file <?php ?> will be parsed as html.

Statement:
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