大致顺序(可以称为贴片法顺序:先组合小的,再组合大的。)
view/contollerName/action视图-》view/layouts/contollerName (这里可能会有view/templates(即controller中setTemplateAfter()初始化的视图,必须放在layouts文件夹))-》 View/index视图。
setTemplateAfter('main')方法是在controller视图之后附加视图main。
class PostsController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
}
public function showAction($postId)
{
// 传递变量$postId给视图
$this->view->setVar("postId", $postId);
}
}
框架在执行PostsController showAction时,会自动在对应的view文件夹找post文件夹,然后找show模版。
Action
View |
views/posts/show.phtml | ACTION视图 |
---|---|---|
Controller
Layout |
views/layouts/posts.phtml | 控制层视图 |
Main
Layout |
views/index.phtml | 默认视图 |
一般来说我们可以把三个模版都写好,但是如果缺了其中的一个模版,系统就会自动往下层渲染。比如没有action模版,系统就自动使用controller的模版。
action视图
<!-- app/views/posts/show.phtml -->
<h3>This is show view!</h3>
<p>I have received the parameter <?php $postId ?></p>
controller的视图
<!-- app/views/layouts/posts.phtml -->
<H2>This is the "posts" controller layout!H2>
<?Php echo $this->getContent() ?>
整个app的视图
<!-- app/views/index.phtml __>
<!-- app/views/index.phtml -->
<Html>
<Head>
<Title>Example</Title>
</Head>
<Body>
<H1>This is main layout!H1>
<?Php echo $this->getContent() ?>
</ Body>
</ Html>
请注意视图文件中调用 $this->getContent() 方法的那一行。这个方法的位置决定内容在 Phalcon\Mvc\View 的层次结构中的上一个视图的哪个位置显示。