CodeIgniter Forums
My_Template class - 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: My_Template class (/showthread.php?tid=14056)



My_Template class - El Forum - 12-16-2008

[eluser]walrus_lt[/eluser]
Hello, i'm trying to create my own tpl class wich allow me to load different tpl's views...
But my OOP experience is very poor Sad I'm getting errors and don't know what to do.

This is my code
Code:
<?php
  // application/project/libraries/My_Template.php
  class My_Template extends CI_Loader
  {
    public static $template;

    function My_Template()
    {
      parent::CI_Loader();
      set_template();
    }

    function set_template($template=null){
      if ($template == null){
      /* TODO later
        if (session){
          self::$template = $session->item('template');
        } else {
      */
        self::$template = $this->config->item('template');
      } else {
        self::$template = $template;
      }
    }

    function load_view($view, $data=array(), $master=0){
      if ($master){
        return $this->load->view(self::$template.'/', $data);
      } else {
        return $this->load->view(self::$template.'/templates/'.$view, $data);
      }
    }
  }

?>

This class was loaded by ci, from autoload array,.... :/ Could some one help to make this class working? :-S


My_Template class - El Forum - 12-16-2008

[eluser]Pascal Kriete[/eluser]
Why are you extending the Loader?

I think you need to take a step back and think about what you want to do. Could you tell us how you want it to work (once it's done) - I'm sure someone will have some pointers. Also, have you read about creating libraries?


My_Template class - El Forum - 12-16-2008

[eluser]ericsodt[/eluser]

If interested in using my idea.. my 'template' system works like struts tiles (j2ee programming - if not familiar)



I have a simple layout file [siteLayout.php]

Code:
<?
    include('header.php');


    $this->load->view($content);


    include('footer.php');
?>


Then from my controllers I 'inject' my content


Code:
$data = array(
               'title' => 'My Page',
               'content' => 'home',
               'breadcrumbs'=>< breadCrumbInfomation >
        );

         $this->load->view('siteLayout',$data);


As you can see I load siteLayout, then inject the contents of home.php into it below the header and above the footer.



This is a real simple way to do templating and works well for me. I hope it does the same for you



Cheers