Reputation: 1950
)
So, I'm fairly new to web development in general, and I've hit sort of a funny problem.
Basically, I'm using the Smarty template engine with PHP to create a basic website. What I currently have is a master template, basically the parts of the website layout that have to be used on every single page of the website.
<!DOCTYPE html>
<html>
<head>
<title>Pet Nicknames!</title>
<link href="styles/main.css" rel="stylesheet">
</head>
<body>
<div id="websiteBody">
<div id="header">
</div>
<div class="menu">
<div class="menuContainer">
{foreach $menuButtons as $menuButton}
<a href="{$menuButton->link}"><div class="menuButton">
<span>{$menuButton->text}</span>
</div></a>
{/foreach}
</div>
</div>
<div id="content">
{block name=content}{/block}
</div>
<div id="footer">
</div>
</div>
</body>
As you can see, this master template includes a foreach which basically just gets an array assigned from the "master.php" script, with an array of buttons retrieved from a database.
The problem is that now I want to create several PHP pages, but they should all re-use this template adn should all have to run the PHP script for retrieving the menu buttons.
I've made a block called "content" as you can see, and I was thinking of using template inheritance to do this... however I'm still unclear of how to properly re-use PHP logic such as for the menu, something that has to be re-used across basically all of the pages on the website, without having to include/repeat it in every PHP script.
What is a "standard" way around this problem in web design? Do you simply encapsulate this "setup" logic in a separate PHP script and include it in all scripts that need to use the master template?
Thank you all for the help!
Upvotes: 0
Views: 213
Reputation: 1668
Cut the <div class="menu">...</div>
and put it in separate template called menu.tpl
now in place of this div ( in main template write ):
{include file="menu.tpl" menuButtons=$menuButtons}
file - The name of the template file to include
menuButtons - variable to pass local to template
Please check this doc for more info:
http://www.smarty.net/docs/en/language.function.include.tpl
Upvotes: 1