用smarty的include函数可以引入一个公共的部分,,,
比如是a.tpl。。我的问题来了,如果a.tpl里头不完全是静态的html,也有一部分是需要assign变量的,那么b.tpl引入a.tpl的时候,
b.tpl对应b.php页面不是还要assgin进来a.tpl里需要的变量嘛。。这不是很麻烦吗?如果一个页面引入的公共模块很多,,,,变量都要拉过来。。

解决方案 »

  1.   

    include_php is a nice way to handle componentized templates, and keep PHP code separate from the template files. Lets say you have a template that shows your site navigation, which is pulled dynamically from a database. You can keep your PHP logic that grabs database content in a separate directory, and include it at the top of the template. Now you can include this template anywhere without worrying if the database information was assigned by the application before hand.
    include_php 是解决模板部件化的好方法,它使得 php 代码从模板文件中被分离出来. 举个例子:假设有一个从数据库中动态取出数据用于显示站点导航的模板,你可以将得数据内容的 php 逻辑部分分离出来保存在一个单独的文件夹下,并在模板开始的位置包含该 php 脚本. 那么就可以在任何地方包含此模板而不用担心之前数据库信息是否已被程序取出.
      

  2.   

    我的做法是对于每个公共的模块写一个自己的.php页面,然后放到一个公共文件夹下面,然后这些.php页面仅仅是assign自己模板页需要的数据不display,然后有哪个页面(index_tpl.html)要包含这些模块就在这个页面的顶部include这个.php页,然后在index_tpl.html里面直接{include file="模块页模板页地址"},这样儿当你需要的时候就include,不用的时候就删掉这个include就行了,对整体页面没有影响