CodeIgniter Forums
Default Template For All Views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Default Template For All Views (/showthread.php?tid=7951)



Default Template For All Views - El Forum - 04-28-2008

[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.


Default Template For All Views - El Forum - 04-29-2008

[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.


Default Template For All Views - El Forum - 04-29-2008

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


Default Template For All Views - El Forum - 04-29-2008

[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.


Default Template For All Views - El Forum - 04-29-2008

[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.


Default Template For All Views - El Forum - 04-29-2008

[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]


Default Template For All Views - El Forum - 04-29-2008

[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


Default Template For All Views - El Forum - 04-29-2008

[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