Welcome Guest, Not a member yet? Register   Sign In
Default Template For All Views
#1

[eluser]shreder[/eluser]
Hello.

I am creating my first application and i have a question.
I want to create a "master" template that will apply to all views.
I will be placing header.php & footer.php inside views/template and i want those files to be used in every single view file without calling them each time.

What's the best solution for this?

Thank you.
#2

[eluser]TheFuzzy0ne[/eluser]
Extend the Controller class, and load it via a helper class which can be loaded in the constructor.

The question is, however, are you absolutely certain this is right for you. If you ever start using Ajax in your application, or if you want to send a download without the entire page reloading, it may have repercussions.
#3

[eluser]xwero[/eluser]
i recommend this library but there are others that can be used for your requirement.
#4

[eluser]shreder[/eluser]
Thank you for your help but i'm still finding it confusing.
I have a difference controller for each section of my site, how do I create a controller that will contain the template structure for all the other controllers?

Thank you.
#5

[eluser]xwero[/eluser]
The easiest solution is to create a library that has a function which contains your template.
Code:
class Customview
{
   var $_ci;
  
   function Customview()
   {
     $this->_ci =& get_instance();
   }

   function front($data)
   {
     $this->_ci->load->view('header');
     $this->_ci->load->view('content',$data);
     $this->_ci->load->view('footer');
   }
}
And use it like this
Code:
class Section extends Controller
{
   function index()
   {
      $this->load->library('customview');
      $data['test'] = 'test';
      $this->customview->front($data);
   }
}
But that is a very static class. There are other libraries that are more dynamic.
#6

[eluser]gtech[/eluser]
create a template library, and then autoload it in config/autoload.php.. I havnt tested it but off the top of my head it might go somthing like:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Template {
    function dotemplate($template, $data)
    {
        $CI =& get_instance();
        $head = $CI->load->view('head','',TRUE);
        $main = $CI->load->view($template, $data,TRUE);
        $foot = $CI->load->view('foot','',TRUE);
        return $head.$main.$foot
    }
}
?>

[edit]ahh beaten to it[/edit]
#7

[eluser]xwero[/eluser]
gtech why do you use the return as string parameter for the view files? from 1.6 let CI do the heavy work for you Smile
#8

[eluser]gtech[/eluser]
I sometimes like making life difficult for myself, it beats playing chicken with the traffic.

Your right no need to return the content if you don't want to, unless you want to do somthing other than display the data (which is probably unlikely)

though it still is a good idea to pass the template file as a parameter into the function




Theme © iAndrew 2016 - Forum software by © MyBB