Reputation: 113
I'm trying to move from AngularJS to PHP Model-View-Controler framework and something doesn't stick to me, because I do to much repeating. In angular you can do simple include of footer and header and all magic happens in view depending od the route. But in Codeiginiter for every view you need to include header and footer. Is there any good practice in which you don't have to do the much repeating
In angularjs i have simple.
<div ng-include='"templates/header.html"'></div>
<div ng-view></div>
<div ng-include='"templates/footer.html"'></div>
And this is routing
.when("/", {
templateUrl: "partials/home.html",
controller: "PageCtrl"
})
// Pages
.when("/home", {
templateUrl: "partials/home.html",
controller: "PageCtrl",
})
.when("/about", {
templateUrl: "partials/about.html",
controller: "PageCtrl"
})
And this is how is done in CodeIgniter
public function home(){
$this->load->view('templates/header');
$this->load->view('about');
$this->load->view('templates/footer');
}
public function about(){
$this->load->view('templates/header');
$this->load->view('index');
$this->load->view('templates/footer');
}
Upvotes: 0
Views: 4268
Reputation: 555
in your controller, you can user simple method as given bellow to specify your header and footer to all yor page.
Controller
class Home extends CI_Controller {
public function index()
{
$headerData = array(
"pageTitle" => "Home",
"stylesheet" => array("home.css")
);
$footerData = array(
"jsFiles" => array("home.js")
);
$viewData = array(
"viewName" => "home",
"viewData" => array(),
"headerData" => $headerData,
"footerData" => $footerData
);
$this->load->view('template',$viewData); // load `Home` view to template file in view folder.
}
View (template.php)
$this->load->view("includes/header.php",$headerData);
$this->load->view($viewName.".php",$viewData);
$this->load->view("includes/footer.php",$footerData);
Upvotes: 0
Reputation: 7111
You can put at the top and on the bottom of files:
View file:
<?php
$this->load->view('templates/header');
// your main file (index;about etc)
$this->load->view('templates/footer')
so, in controller you could load just main file i.e.
Controller file:
public function home()
{
// $this->load->vars($data)
$this->load->view('index');
}
Also study Loader class to know how to set variables for all of those files.
Upvotes: 1