[eluser]Edemilson Lima[/eluser]
Thank you for the answer! Your template class is nice, but I don't know if it is exactly what I need. I will try to demonstrate my problem with some examples.
In the old days, I was used to load everything using include() or require(), like this:
index.php:
Code:
<?php
require('mylibrary/index.php'); // starts session, validades user, load helpers, etc.
if(!$section) $section='Home';
?>
<html>
<head>
<title>Web Site Title - <?=$section?></title>
<style rel="stylesheet" href="layout/mystyles.css" type="text/css" />
</head>
<body>
<div id="header"><?php require('header.php'); ?></div>
<div id="lateral"><?php
if(!$lateral) $lateral='categories';
require('lateral.php');
?></div>
<div id="contents"><?php
if(!$contents) $contents='home.php';
require("{$contents}.php");
?></div>
<div id="footer"><?php require('footer.php'); ?></div>
</body>
</html>
header.php:
Code:
<img src="layout/logomark.gif"><br />
<?php require('menu.php'); ?>
lateral.php:
Code:
blah blah blah<br />
<?php require("{$lateral}.php"); ?>
categories.php:
Code:
<?php
// code to load a list of categories from a database and display it
?>
home.php:
Code:
<div id="news"><?php require('news.php'); ?></div>
<div id="offers"><?php require('offers.php'); ?></div>
news.php:
Code:
<?php
// code to load news from a database or RSS and display them
?>
offers.php:
Code:
<?php
// code to load offers from a database and display them
?>
footer.php:
Code:
<div id="footer">Credits, some links, etc</div>
products.php:
Code:
<?php
if($action=='add') {
// code to include a new product
}
if($action=='update') {
// code to update a product
}
if($action=='remove') {
// code to remove a product from the database
}
if(!$action or $action=='list') {
// code to show the product list
}
?>
As you can see, when index.php is executed, it loads the blocks, which loads their sub blocks and so on. Everything is done automatically, as one is inside another. I don't need to rebuild every block at every request.
If I pass to the variable $content the value 'products', the index.php will include the 'products.php' instead of 'home.php'. In the case of product.php, I can choose which process will be executed, changing the $action variable in some way. I know, this is pure procedural, but it works.
Well, lets do it in the CodeIgniter way:
welcome.php:
Code:
<?php
class Welcome extends Controller {
function Welcome() {
parent::Controller();
$this->load->model('categories');
$this->load->model('news');
$this->load->model('offers');
}
function index() {
$header['menu']=$this->load->view('menu_view','',true);
$categories=$this->categories->list();
$lateral['lateral']=$this->load->view('categories_view',$categories,true);
$news=$this->model->news->list();
$home['news']=$this->load->view('home_view',$news,true);
$offers=$this->model->offers->list();
$home['offers']=$this->load->view('offers_view',$offers,true);
$data['section']='Home';
$data['header']=$this->load->view('header_view',$header,true);
$data['lateral']=$this->load->view('lateral_view',$lateral,true);
$data['contents']=$this->load->view('home_view',$home,true);
$data['footer']=$this->load->view('footer_view','',true);
$this->load->view('main_view',$data);
}
}
?>
main_view:
Code:
<html>
<head>
<title>Web Site Title - <?=$section?></title>
<style rel="stylesheet" href="layout/mystyles.css" type="text/css" />
</head>
<body>
<div id="header"><?=$header?></div>
<div id="lateral"><?=$lateral?></div>
<div id="contents"><?=$contents?></div>
<div id="footer"><?=$footer?></div>
</body>
</html>
header_view:
Code:
<img src="layout/logomark.gif"><br />
<?=$menu?>
lateral_view:
Code:
blah blah blah<br />
<?=$lateral?>
home_view:
Code:
<div id="news"><?=$news?></div>
<div id="offers"><?=$offers?></div>
Well, I think is not necessary to show news_view, offers_view or footer_view here. Please continue reading the next post.