编程语言
首页 > 编程语言> > 3.10.2CakePHP Layouts

3.10.2CakePHP Layouts

作者:互联网

原文链接:http://www.cnblogs.com/xinyebabao/archive/2011/06/22/2087071.html

3.10.2 Layouts

Layout文件应该放在/app/views/layouts下。如果我们在这个路径下新建一个default.ctp,那么CakePHP默认的layout(布局)就会被掩盖掉。当然,创建新layout时,我们要告诉CakePHP将我们的views代码放在哪里。 为了实现此功能, 需确保有个地方放置$content_for_layout (可选:$title_for_layout). 下面是个例子,简单叙述一个默认layout的样子(这只是一种可能,你可以随意改动):

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3  <html xmlns="http://www.w3.org/1999/xhtml">
4  <head>
5 <title><?php echo $title_for_layout?></title>
6 <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
7 <!-- Include external files and scripts here (See HTML helper for more info.) -->
8 <?php echo $scripts_for_layout ?>
9 </head>
10 <body>
11
12 <!-- If you'd like some sort of menu to
13 show up on all of your views, include it here -->
14 <div id="header">
15 <div id="menu">...</div>
16 </div>
17
18 <!-- Here's where I want my views to be displayed -->
19 <?php echo $content_for_layout ?>
20
21 <!-- Add a footer to each displayed page -->
22 <div id="footer">...</div>
23
24 </body>
25 </html>

<!--  

  $scripts_for_layout contains any external files and scripts included with the built-in HTML helper. Useful for including javascript and CSS files from views.

  When using $html->css() or $javascript->link() in view files, specify 'false' for the 'in-line' argument to place the html source in $scripts_for_layout. (See API for more details on   usage).

  $content_for_layout contains the view. This is where the view code will be placed.

  $title_for_layout contains the page title.

-->

想要改变一个页面的title的话,最简单的方法是设置 $title_for_layout 变量:

<?php

class UsersController extends AppController {
function viewActive() {
$this->set('title_for_layout', 'View Active Users');
}
}
?>

这样,url为localhost/users/viewActive的页面的title就是title_for_layout啦。

只要在 项目名\app\views\layouts\里加入default.ctp,就可以覆盖默认的 项目名\cake\libs\view\layouts\default.ctp了。你还可以新建任意多layouts文件,只要都放置在app/views/layouts下就没问题!你会问:那么多layouts,我怎么知道该调用哪个。没错,你应该知道你要调用哪个——仅仅用controller的$layout变量就行!(也可以使用setLayout()方法)。这里只记录了变量的方法。例子:

我的viewActive方法用到视图default_small_ad,而viewImage方法用到视图image,因此简单的用下面代码就实现了:

<?php
class UsersController extends AppController {
function viewActive() {
$this->set('title_for_layout', 'View Active Users');
$this->layout = 'default_small_ad';
}

function viewImage() {
$this->layout = 'image';
//output user image
}
}
?>
 
 <!--

转载于:https://www.cnblogs.com/xinyebabao/archive/2011/06/22/2087071.html

标签:2CakePHP,layout,views,3.10,title,layouts,default,Layouts,view
来源: https://blog.csdn.net/weixin_30613343/article/details/96843514