【问题标题】:Smarty templating inheritence - passing variables?Smarty模板继承 - 传递变量?
【发布时间】:2011-08-24 06:07:04
【问题描述】:

好的开始我使用的是最新的 Smarty 版本,一切都设置好了。

我的问题是我有一个名为 layout.tpl 的整个网站的主模板,然后是 home.tpl、about.tpl 等每个页面的子模板...

index.php(家用)

require('Smarty.class.php');
// --
$smarty = new Smarty;
$smarty->template_dir = 'C:/xampp/htdocs/EMP3/view/templates';
$smarty->config_dir = 'C:/xampp/htdocs/EMP3/view/config';
$smarty->cache_dir = 'C:/xampp/smarty/cache';
$smarty->compile_dir = 'C:/xampp/smarty/templates_c';
$smarty->display('home.tpl');

主页.tpl

{extends file="layout.tpl"}
{block name=title}Home{/block}
{block name=body}
<div>Sample</div>
{/block}

布局.tpl

// Long html file which makes use of the blocks from home, about ect...

我的问题是 layout.tpl 中的 html 内容是隐藏还是显示取决于用户是管理员还是普通用户。如何将这些 PHP 值传递给 layout.tpl?通过 home.tpl?有没有更好的方法来实现这一点?

谢谢

【问题讨论】:

    标签: php smarty templating


    【解决方案1】:

    我没有使用 Smarty v3 和 Inheritance,只是继续上面的示例代码,但是 assign() 怎么样?我知道当您有一个包含另一个文件的模板文件时,所有分配的变量仍然有效,并且能够在 {include} 语句中分配新变量。

    index.php(家用)

    require('Smarty.class.php');
    // --
    $smarty = new Smarty;
    $smarty->template_dir = 'C:/xampp/htdocs/EMP3/view/templates';
    $smarty->config_dir = 'C:/xampp/htdocs/EMP3/view/config';
    $smarty->cache_dir = 'C:/xampp/smarty/cache';
    $smarty->compile_dir = 'C:/xampp/smarty/templates_c';
    
    $smarty->assign('some_header', 'hello!');
    
    $smarty->display('home.tpl');
    

    Home.tpl(不变)

    布局.tpl

    <html>
        ...
        <h1>{$some_header}</h1>
        // Long html file which makes use of the blocks from home, about ect...
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 2016-12-03
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多