首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Smarty模板继承传递变量?

问Smarty模板继承传递变量?
EN

Stack Overflow用户
提问于 2011-05-12 23:46:32
回答 1查看 1.3K关注 0票数 2

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

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

index.php (用于家庭)

代码语言:javascript
复制
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');

Home.tpl

代码语言:javascript
复制
{extends file="layout.tpl"}
{block name=title}Home{/block}
{block name=body}
<div>Sample</div>
{/block}

Layout.tpl

代码语言:javascript
复制
// Long html file which makes use of the blocks from home, about ect...

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

谢谢

EN

回答 1

Stack Overflow用户

发布于 2011-05-13 21:51:43

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

index.php (用于家庭)

代码语言:javascript
复制
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 (未更改)

Layout.tpl

代码语言:javascript
复制
<html>
    ...
    <h1>{$some_header}</h1>
    // Long html file which makes use of the blocks from home, about ect...
</html>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5980709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档