公共模板和模板布局

公共模板
引用其他的模板文件使用include标签

<include file='模版表达式或者模版文件1,模版表达式或者模版文件2,...' />

使用规则

模块@主题/控制器/操作
<include  file='Public/header'/>
    hello,{$name}!
<include  file='Public/footer'/>

可引入多个文件

<include  file='Public/header,Public/menu'/>

直接使用模板文件 包含完整路径:

<include file="./Application/Home/View/default/Public/header.html" />

模板传参

<include  file='Public/header,Public/menu' title='ThinkPHP TITLE' keywords="开源WEB开发框架"/>

模板中使用参数

<html>
<head>
<title>[title]</title>
<meta name="keywords" content="[keywords]" />
</head>
<body>
<h1>Header</h1>

模板布局
ThinkPHP配置文件中

'TMPL_LAYOUT_ITEM'      =>  '{__CONTENT__}', // 布局模板的内容替换标识
    'LAYOUT_ON'             =>  true, // 是否启用布局
    'LAYOUT_NAME'           =>  'layout', // 当前布局名称 默认为layout

开启以后并在View目录下创建layout.html

<include  file='Public:header,Public:menu' title='ThinkPHP TITLE' keywords="开源WEB开发框架"/>
   {__CONTENT__}
<include  file='Public:footer'/>

读取模板文件后,会解析index.html模板文件,并把解析后的内容替换到layout布局中的{CONTENT}
指定其他布局模板

'LAYOUT_NAME'=>'Layout/layoutname',

特定页面不需要布局模板功能,可以在模板文件开头加上{NOLAYOUT}字符串

{__NOLAYOUT__}
hello,{$name}!

模板标签方式
不需要配置任何参数,也不需要开启LAYOUT_ON,直接在模板文件中指定布局模板即可。

关闭配置文件中:LAYOUT_ON=false
<layout name='layout'/>
hello,{$name}!

使用layout控制模板布局
动态开启模板

namespace Home\Controller;
use Think\Controller;
Class UserController extends Controller {
    Public function add() {
        layout(true);
        $this->display('add');
    }
 }

指定不同的模板文件

<?php
namespace Home\Controller;
use Think\Controller;
Class UserController extends Controller {
    Public function add() {
        layout('Layout/newlayout');
        $this->display('add');
    }
 }

动态关闭模板

<?php
namespace Home\Controller;
use Think\Controller;
Class UserController extends Controller {
    Public function add() {
        layout(false); // 临时关闭当前模板的布局功能
        $this->display('add');
    }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值