Yii中的layouts使用(header,footer的使用方式)

1. config/main.php里增加layout字段
return array(
'defaultController'=>'index',
'layout' => 'main',
2. layouts里创建3个文件, main.php, header,footer
main.php文件内容( index.php会做为$content输出,render 设置哪个模板就显示哪个)
< ?php $this - >beginContent( '//layouts/header'); ? > < ?php $this - >endContent(); ? > < ?php echo $content; ? > <br >main.php -- --
< ?php $this - >beginContent( '//layouts/footer'); ? > < ?php $this - >endContent(); ? >
header.php和footer.php分别输出
header.php---和<br>footer---
3. views/index.php里做简单输出
<br><?php echo $post->m; ?>
4. C 控制
$post->m = 'index----';
$this->render('index', array(
      'post' => $post
));
5最终执行结果
header.php--- index---- main.php---- footer.php---
!!!  如果控制器里使用 renderPartial  !!!
$this->renderPartial('index', array(
'post' => $post
));
则只输出
index----
不调用layouts
批注:render会渲染layout,而renderPartial不会渲染。

关于layouts传参
需要在控制器里定义一个变量
public $a = null;
然后在Action里赋值
然后就可以在layout里调用了
以下是代码
复制代码
class IndexController extends CController
{
    public $a = null;
    public function actionIndex()
    {
        $post->m = 'index----';
        $this->a = 'hello a!';
        $this->render('index', array(
            'post' => $post,
        ));
复制代码

layouts.main.php里调用方法
<?php echo $this->a; ?>
输出结果:hello a!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值