Smarty templating inheritence - passing variables?
Alright to start off I am using the latest Smarty version, and everything is setup.
My problem is that I have a main template for the whole site called layout.tpl, then sub-templates for each of the pages home.tpl, about.tpl ect...
index.php (for home)
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
{extends file="layout.tpl"}
{bloc开发者_JAVA技巧k name=title}Home{/block}
{block name=body}
<div>Sample</div>
{/block}
Layout.tpl
// Long html file which makes use of the blocks from home, about ect...
My problem is that the html content in layout.tpl is hidden or shown depending on whether the user is an admin or normal user. How do I pass in these PHP values to layout.tpl? Through home.tpl? Is there a better way to achieve this?
Thanks
I haven't used Smarty v3 and Inheritance and just going on your example code above, but how about assign()
? I know when you have a template file that includes another file, all assigned variables are still valid, as well as being able to assign new ones in the {include}
statement.
index.php (for home)
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 (unchanged)
Layout.tpl
<html>
...
<h1>{$some_header}</h1>
// Long html file which makes use of the blocks from home, about ect...
</html>
精彩评论