Home > php教程 > php手册 > body text

ThinkPHP3.1新特性之内容解析输出详解

WBOY
Release: 2016-06-13 09:30:11
Original
881 people have browsed it

以往版本的ThinkPHP中页面输出的过程是读取模板文件,然后进行模板解析(也支持调用第三方模板引擎解析),但是有一些情况,我们并没有定义模板文件,或者把模板文件保存在数据库里面,那么这种情况下进行页面输出的时候,我们是无法进行模板文件读取的,ThinkPHP3.1版本则针对这样的情况增加了内容解析输出的功能。

内置的模板引擎也进行了完善,如果传入的模板文件不存在的话,则会认为是传入的模板解析内容,因此,ThinkPHP3.1版的View类和Action类也做了一些相应的改进。

display方法用于模板文件渲染输出,show方法则用于模板内容渲染输出,并且show方法仍然支持内容解析功能,因此,我们可以在控制器中这样使用:

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

Copy after login

页面输出的结果是:

hello,ThinkPHP!

Copy after login

你也可以采用读取数据库的方式:

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

Copy after login

对于$content变量中的内容,可以和模板文件一样支持变量和标签库的解析,并且也可以支持模板布局功能。
show方法也可以指定输出编码和类型,例如:

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

Copy after login

总之,有了show方法后,可以把模板放数据库中,对于模板的管理和更新也更加方便了。

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 Recommendations
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!