ThinkPHP3.1快速入门(15)模板布局

ThinkPHP的模板引擎内置了布局模板功能支持,可以方便的实现模板布局以及布局嵌套功能。
有三种布局模板的支持方式:

第一种方式:全局配置方式

这种方式仅需在项目配置文件中添加相关的布局模板配置,就可以简单实现模板布局功能,比较适用于全站使用相同布局的情况,需要配置开启LAYOUT_ON参数(默认不开启),并且设置布局入口文件名LAYOUT_NAME(默认为layout)。

‘LAYOUT_ON’=>true,
‘LAYOUT_NAME’=>’layout’,

开启LAYOUT_ON后,我们的模板渲染流程就有所变化,例如:

Class UserAction extends Action {
    Public function add() {
        $this->display(‘add’);
    }
}

在不开启LAYOUT_ON布局模板之前,会直接渲染Tpl/User/add.html模板文件,开启之后,首先会渲染Tpl/layout.html模板,布局模板的写法和其他模板的写法类似,本身也可以支持所有的模板标签以及包含文件,区别在于有一个特定的输出替换变量{__CONTENT__},例如,下面是一个典型的layout.html模板的写法:

<include file=”Public:header” />
{__CONTENT__}
<include file=”Public:footer” />

读取layout模板之后,会再解析User/add.html模板文件,并把解析后的内容替换到layout布局模板文件的{__CONTENT__}特定字符串。
当然可以通过设置来改变这个特定的替换字符串,例如:

‘TMPL_LAYOUT_ITEM’      =>  ‘{__REPLACE__}’

但是一个布局模板同时只能有一个特定替换字符串。
采用这种布局方式的情况下,一旦User/add.html模板文件或者layout.html布局模板文件发生修改,都会导致模板重新编译。
如果需要指定其他位置的布局模板,可以使用:

‘LAYOUT_NAME’=>’Layout/layoutname’,

就表示采用Tpl/Layout/layoutname.html作为布局模板。

如果某些页面不需要使用布局模板功能,可以在模板文件开头加上{__NOLAYOUT__}字符串。
如果上面的User/add.html模板文件里面包含有{__NOLAYOUT__},则即使当前开启布局模板,也不会进行布局模板解析。

第二种方式:模板标签方式

这种布局模板不需要在配置文件中设置任何参数,也不需要开启LAYOUT_ON,直接在模板文件中指定布局模板即可,相关的布局模板调整也在模板中进行。
以前面的输出模板为例,这种方式的入口还是在User/add.html模板,但是我们可以修改下add模板文件的内容,在头部增加下面的布局标签(记得首先关闭前面的LAYOUT_ON设置,否则可能出现布局循环):

<layout name=”layout” />

表示当前模板文件需要使用layout.html布局模板文件,而布局模板文件的写法和上面第一种方式是一样的。当渲染User/add.html模板文件的时候,如果读取到layout标签,则会把当前模板的解析内容替换到layout布局模板的{__CONTENT__}特定字符串。
一个模板文件中只能使用一个布局模板,如果模板文件中没有使用任何layout标签则表示当前模板不使用任何布局。
如果需要使用其他的布局模板,可以改变layout的name属性,例如:

<layout name=”newlayout” />

还可以在layout标签里面指定要替换的特定字符串:

<layout name=”Layout/newlayout”  replace=”{__REPLACE__}” />

由于所有include标签引入的文件都支持layout标签,所以,我们可以借助layout标签和include标签相结合的方式实现布局模板的嵌套。例如,上面的例子

<include file=”Public:header” />
<div id=”main” class=”main” >
{__CONTENT__}
</div>
<include file=”Public:footer” /> 

在引入的header和footer模板文件中也可以添加layout标签,例如header模板文件的开头添加如下标签:

<layout name=”menu” />

这样就实现了在头部模板中引用了menu布局模板。
也可以采用两种布局方式的结合,可以实现更加复杂的模板布局以及嵌套功能。

第三种方式:使用layout控制模板布局

使用内置的layout方法可以更灵活的在程序中控制模板输出的布局功能,尤其适用于局部需要布局或者关闭布局的情况,这种方式也不需要在配置文件中开启LAYOUT_ON。例如:

Class UserAction extends Action {
    Public function add() {
        layout(true);
        $this->display(‘add’);
    }
}

表示当前的模板输出启用了布局模板,并且采用默认的layout布局模板。
如果当前输出需要使用不同的布局模板,可以动态的指定布局模板名称,例如:

Class UserAction extends Action {
    Public function add() {
        layout(‘Layout/newlayout’);
        $this->display(‘add’);
    }
}

或者使用layout方法动态关闭当前模板的布局功能(这种用法可以配合第一种布局方式,例如全局配置已经开启了布局,可以在某个页面单独关闭):

Class UserAction extends Action {
    Public function add() {
        layout(false); // 临时关闭当前模板的布局功能
        $this->display(‘add’);
    }
}

总结

三种模板布局方式中,第一种和第三种是在程序中配置实现模板布局,第二种方式则是单纯通过模板标签在模板中使用布局。具体选择什么方式,需要根据项目的实际情况来了。

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » ThinkPHP3.1快速入门(15)模板布局

31 评论

  1. cialis 40mg over the counter price cialis buy sildenafil pills

  2. purchase zantac online cheap celebrex uk buy celebrex 200mg sale

  3. 9Вє – Maldonado – O zagueiro e volante chileno foi contratado pelo Tricolor em 2000, quando o clube gastou 8,5 milhГµes de euros (15,6 milhГµes de reais Г  Г©poca), para tirar o jogador do Colo-Colo Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information. O Rubro-Negro do Rio de Janeiro terminou o primeiro tempo com mais posse de bola e finalizações. A primeira oportunidade do Leão veio aos 47 minutos, quando Pedro tocou contra o próprio gol em cobrança de falta lateral e obrigou o goleiro Diego Alves a colocar para escanteio. Porém, o placar se manteve até o intervalo.
    https://miriam.net.pl/community/profile/svenjamison873/
    Mas ainda no primeiro tempo, novamente pГЄnalti. Dagson fez o terceiro com uma cobranГ§a cavadinha aos 44 minutos. JГЎ aos 5 minutos do segunto tempo, TarcГ­sio balanГ§ou a rede e fez o segundo dos mandantes. Mas ainda no primeiro tempo, novamente pГЄnalti. Dagson fez o terceiro com uma cobranГ§a cavadinha aos 44 minutos. JГЎ aos 5 minutos do segunto tempo, TarcГ­sio balanГ§ou a rede e fez o segundo dos mandantes. Apenas a Rede Globo manteve os direitos de transmissão da competição. Os jogos serão transmitidos na TV aberta pela Globo nas quartas e domingos, além dos canais fechados da emissora: SporTV e Premiere, com transmissão integral.  Notícia em constante atualização até o final da rodada

  4. Spain – Primera RFEF – Group 2 Ahead of the 2023 Supercoppa Italiana between Milan and Inter, we take a deep dive into everything there is to know about the Italian Super Cup. While the form book is one that is often thrown out of the window in a competition like the Premier League, it is always worth studying the form of the two teams along with which way historic results have gone. Many correct score tips will delve into the record books before posting their correct score predictions. However, just because a side has beaten their opponent 2-1 in four of their last 10 meetings going back 20 years does not mean they will do it again. Ensure you are not following correct score tips from a lazy writer that has not done their research properly or, similarly, don’t curtail your own chances of winning by not doing the relevant research before you place your money.
    https://www.udrpsearch.com/user/rebeccagreen
    Claiming that soccer predictions are guaranteed are not our thing. And to clear this out: nobody should claim something like that. Services that say their betting predictions are guaranteed do not properly stand behind their claim as they do not refund your staked money in case of loss nor do they offer to pay you the winnings if the prediction would have been won. The most many do is offer back the fee you paid to get the prediction, which is not really a guarantee for the quality of the soccer prediction. Bettors are probably not strange with this content, which provides trustworthy statistics and information for football pre-matches, allowing them to place more informed and accurate bets. Betimate always creates detailed match previews with comprehensive stats and analysis to help readers make more informed betting decisions. 

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡