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)模板布局

79 评论

  1. order prograf without prescription prograf 1mg pills ursodiol for sale

  2. order cialis 10mg generic Cialis in usa buy sildenafil 100mg without prescription

  3. buy medroxyprogesterone online cheap generic provera 5mg order periactin pill

  4. buy modafinil 100mg canada pharmacy ivermectin for people

  5. olanzapine 10mg generic diovan 80mg generic buy valsartan without prescription

  6. brand linezolid 600mg casinos online online casino for real cash

  7. buy cialis for sale oral viagra sildenafil ca

  8. purchase flomax generic oral zofran 8mg buy aldactone 25mg

  9. online slots real money stromectol ca provigil 200mg price

  10. order terbinafine 250mg online oral lamisil order amoxicillin 250mg pills

  11. Jouer au casino en ligne sur les sites agréés de jeux d’argent autorisés par la France En France, il vous est impossible d’accéder aux sites des casinos qui ne détiennent pas de licence. De plus, les joueurs français qui jouent sur une plateforme de jeu non homologuée et reconnue par les autorités locales peuvent s’exposer à des sanctions pénales. Enfin, en jouant sur un casino illégal, il va de soi que vous ne pourrez pas vous plaindre auprès des mêmes autorités en cas d’arnaque. Les casinos en ligne connaissent depuis des années une véritable popularité. Toutefois, les gouvernements ne voient pas cela d’un très bon œil. Les raisons de cette interdiction des jeux de hasard en ligne sont assez diverses et dépendent d’un État à l’autre. Alors que certains pays interdisent totalement les jeux d’argent, d’autres tentes d’imposer des réglementations afin de réguler au maximum ce genre de pratiques.
    https://titussyqi321098.vidublog.com/16812118/site-de-poker-en-ligne-gratuit
    Essayez-vous sur Big Bass Bonanza et tentez de gagner le jackpot sur ce slot à 5 rouleaux et 10 lignes de paiement. Avec cette machine à sous, vous avez la possibilité de décrocher des poissons-jackpot et de gagner jusqu’à x2100 votre mise initiale. Le jeu est relativement simple et fun. La stratégie ultime pour gagner aux machines à sous est évidemment liées aux mises. Comme dans tout jeu de casino, jouer rime souvent avec miser. Et à chaque jeu son types de mises. Quelles sont les types de mises et comment miser aux machines à sous? Nous avons consacré un article à cette question dont dépend beaucoup l’optimisation de votre jeu. Dans cet article nous vous parlerons taux de redistribution, choix de la machine à sous, choix des options de jeu, mais aussi lignes de gain …

  12. Kerala Blasters will take on Goa on Sunday, November 13, at Jawaharlal Nehru International Stadium (Kochi, Kerala).If you are looking for KBFC vs FCG Dream11 Fantasy Football Prediction then you are at the right place. #6 Is there a playoff round in Indian Super League after the regular season?There isn’t a playoff competition after the regular season in Indian Super League. Doha: Spain put on a footballing masterclass … M01 | OCT 14, 2022 | Chennaiyin FC 1-1 Bengaluru FC, Match Report Simon Grayson’s side went into the new ISL season on the back of a first Durand Cup title. With fans once again allowed into the stadiums, expectations from the 2018-’19 champions were high. There was a new hope. However, the pre-season buzz has not translated into results on the pitch. There goes the final whistle and East Bengal draw a blank on the Kolkata Derby. ATKMB found the net twice within 10 minutes in the second half, and that is more than enough for them for a memorable night. 
    https://tiny-wiki.win/index.php?title=Mobile_flash_score_soccer_results
    The UEFA word, the UEFA logo and all marks related to UEFA competitions, are protected by trademarks and/or copyright of UEFA. No use for commercial purposes may be made of such trademarks. Use of UEFA.com signifies your agreement to the Terms and Conditions and Privacy Policy. Email address not available. Always keeping abreast of the events in the football world is a task for true fans. With livescores.biz it becomes much easier, because all football today results are published on one site, which will allow you not to miss an interesting event. ©Tue Nov 29 22:44:10 CET 2022 xscores.com All rights reserved. The Women’s ITF World Tennis Tour offers approximately 500 tournaments in over 60 countries worldwide and incorporates five prize money levels of tournaments: 15s – offering $15,000 in prize money; 25s – offering $25,000 in prize money; 60s – offering $60,000 in prize money; 80s – offering $80,000 in prize money; 100s – offering $100,000 in prize money.

  13. buy acetazolamide 250 mg generic imuran ca buy azathioprine 25mg without prescription

  14. generic nortriptyline paxil oral order paroxetine sale

  15. UFC 284 Betting I’m between BetOnline and 5dimes based on feedback so far. BetOnline GUI looks nicer, but I don’t really care that much about it. Copyright © 2023 Penske Business Media. All rights reserved. Horse Racing Betting – Where To Bet on the Horses Of course, to top the list is one of the best sports betting sites and the official sportsbook of the UFC, the DraftKings Sportsbook. This deal (made in 2021) gives DraftKings exclusive rights to particular promotions for UFC events, boosted odds, in-broadcast odds for live betting, and UFC branding and marketing assets for the sports betting platform. DraftKings has been a household name in the US for daily fantasy sports since 2012 and eventually launched one of the country’s biggest sportsbooks betting apps in late 2018 after changes in legislation were made earlier that year.
    https://subscribe.ru/author/30778294
    Put it all together, and I’m excited to grab +100 odds at FanDuel. You can also find +100 odds at BetRivers, but DraftKings and Pointsbet have the over priced out to -110 and -115. Last updated: Feb 7, 2:56 PM UTC We got some NBA player props for you! Eight NBA games are on the program this evening, meaning that there are literally thousands of player props you can bet on. We’re here to help you choose the winning ones with our top three picks for tonight’s games. Clippers vs Nets How to Watch Odds Prop All odds via DraftKings Sportsbook Best NBA Bets Today | NBA Picks February 7 Analyzing Tuesday’s Cincinnati at Tulane odds and lines, with college basketball expert picks, predictions and best bets. Check out our best 76ers vs. Knicks picks based on the top NBA odds. My Pick: Suns at Pistons Over

发表评论

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

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