Detailed explanation of content parsing output of new features of ThinkPHP 3.1

  • 2021-07-02 23:39:17
  • OfStack

In previous versions of ThinkPHP, the process of page output was to read the template file. Then perform template parsing (calling the third-party template engine for parsing is also supported), However, in some cases, we don't define the template file, or save the template file in the database, so we can't read the template file when outputting the page in this case. ThinkPHP3.1 version adds the function of content parsing and output for this situation.

The built-in template engine has also been improved. If the incoming template file does not exist, it will be considered as the incoming template parsing content. Therefore, View and Action classes in ThinkPHP version 3.1 have also made some corresponding improvements.

The display method is used for template file rendering output, the show method is used for template content rendering output, and the show method still supports content parsing, so we can use it in the controller as follows:


$this->assign('name','ThinkPHP');
$this->show('hello,{$name}!');

The result of the page output is:


hello,ThinkPHP!

You can also read the database:


$content = M('Data')->where($map)->getField('content');
$this->show($content);

For the contents of $content variables, parsing of variables and tag libraries can be supported like template file 1, and template layout function can also be supported.
The show method can also specify the output encoding and type, for example:


$this->show($content,'utf-8','text/xml');

In a word, with show method, you can put templates in the database, and it is more convenient to manage and update templates.


Related articles: