Skip to content
空樱酱 edited this page Oct 30, 2015 · 7 revisions

视图

视图引擎采用原生PHP的方式,并不是用模板语言。

通过Controller类的assign来给模板变量赋值,通过display方法来渲染模板。

assign方法接受两个参数,第一个参数是模板变量名,第二个参数是模板变量值。

display方法可以接受1个或0个参数。当没有参数时,则默认使用View/控制器名/Action名.html作为模板;如果参数值不带有'/',则默认使用View/控制器名/参数值.html作为模板;如果参数值带有1个'/',则会使用View/参数值.html作为模板。

示例:

class IndexController extends Controller { 
    public function indexAction(){
        $this->assign('var', 'hello Kotori'); //给模板变量赋值
        $this->display();           //使用View/Index/index.html作为模板
    }
    public function TestAction(){
        $this->display('fuck/test');    //使用View/fuck/test.html作为模板
    }
}

模板继承功能,通过View::need来引入其他模板,该方法接受1个或2个参数,第一个参数是模板,规则与display的参数相同,第二个参数是传递给该模板的模板变量,必须是关联型数组,一般第二个参数无需传入

比如有一个公共header文件位于View/Public/header.html:

<head>
    <title><?php echo $title;?></title>
</head>

我们需要在主页面中引入该头部模板:

<html>
<?php View::need('Public/header', $data); ?>
<body>
    ...
Clone this wiki locally