简体   繁体   English

PHP Smarty tpl,为所有页面调用相同的php代码

[英]PHP Smarty tpl, call the same php code for all page

So I want to use smarty similar to how asp.net does master pages, that is I want to crate a master.tpl file, that takes care of consistently rendering most of the page(headers, footers, backgrounds, etc), however I have a drop down menu that I want on every page, and it has some dynamic data, I would like not call or reproduce this function in every php script that uses the master.tpl file, I would like a way that when ever that tpl file is used the correct scripts are run in addition. 因此,我想使用类似于asp.net处理母版页的方式,即我要创建一个master.tpl文件,该文件负责始终如一地呈现大多数页面(页眉,页脚,背景等),但是我我希望在每个页面上都有一个下拉菜单,并且它具有一些动态数据,我不希望在每个使用master.tpl文件的php脚本中调用或复制此函数,我希望有一种方法,只要该tpl使用该文件还会另外运行正确的脚本。

I'm looking to do this the correct way, so not using {php} or anything else that is deprecated. 我正在寻找正确的方法,所以不要使用{php}或其他任何不推荐使用的方法。

thanks. 谢谢。

Render a seperate smarty template and add it in a config file 渲染一个单独的smarty模板并将其添加到配置文件中

$smarty->assign('items',getmenuitems());


$menu = $smarty->fetch('mymenu.tpl');

$smarty->assign('dynamic_menu',$menu);

If you put code like this in a config file etc that's called on each page it should do what you want. 如果将这样的代码放在每个页面上调用的配置文件等中,它应该执行您想要的操作。 Then {$dynamic_menu} can be put in your main template. 然后可以将{$ dynamic_menu}放入您的主模板中。

On another note I find that using a different delimiter for smarty removes the pain of using it worth scripts etc. 另一方面,我发现为smarty使用不同的分隔符消除了使用值得脚本等的麻烦。

$smarty-> left_delimiter = '{[';

Just include it via smarty call. 只需通过smarty调用即可将其包括在内。 {include file="path" name_of_variable=$value_of_variable}. {include file =“ path” name_of_variable = $ value_of_variable}。 In php code just do additional code that data would be generated on every page. 在php代码中,只需执行其他代码即可在每个页面上生成数据。

因此,我最终编写了一个插件(比我最初想象的要简单得多),并且只返回了一个包含<li>项的字符串,我想知道是否有可能在foreach循环中调用此函数,昨晚不上班。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM