Welcome Guest, Not a member yet? Register   Sign In
a general include
#1

[eluser]dotweb[/eluser]
First of all hi everybody, second, if it is the wrong place to ask the following question, please move'it where it should.

Now, for my dilema: i need someone to explain to me how can i include, all over a website (made with CI) a header, a footer or any other block of html (but which it has a logic behind, dynamic content and such). How should i write the controller so it does include those blocks everywhere ? For each block that will be included, i think i need to make a controller (for that logic i was talking about above) ? How do i call that controller on each page in the website (page which is actually a controller by itself) ?

Thank you in advance for your patience and time to answer my questions.
#2

[eluser]Matthew Pennell[/eluser]
Hi - welcome to CI. Smile

Firstly, you don't need a controller for every block of HTML, even if they do contain dynamic content. Let's say you want to load a page that has a header and footer - you would create your controller, populate your dynamic content and send it to your view:

CONTROLLER
Code:
class Test extends Controller
{
    function Test()
    {
        parent::Controller();
    }

    function index()
    {
        $data['page_title'] = 'Welcome To My Site';
        $data['footer'] = '© 2007 dotweb';
        $data['content'] = 'Hello everyone!';
        $this->load->view('template', $data);
    }
}

Then, your 'template' view will load the header and footer views from within itself:

VIEW: template.php
Code:
<?php
$this->load->view('header');
?>

<h1>&lt;?= $page_title ?&gt;</h1>

<p>&lt;?= $content ?&gt;</p>

&lt;?php
$this->load->view('footer');
?&gt;

And your header and footer included views can interpret the dynamic data from the controller:

VIEW: header.php
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?= $page_title ?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
VIEW: footer.php
Code:
<p>&lt;?= $footer ?&gt;</p>

&lt;/body&gt;
&lt;/html&gt;

It all gets stuck together to build a complete page that is sent to the browser.

There are other ways of doing this, including a Rails-like Yield approach using hooks which I've found very useful.
#3

[eluser]nirbhab[/eluser]
I had the same query few hours before, thank you for the solution,
i wud love 2 extend some more query in this:

what is we want to add a 'login' controller in this view page?
obviously it wud be a form having necessary functionality.
#4

[eluser]dotweb[/eluser]
[quote author="Buddy Bradley" date="1192558082"]
And your header and footer included views can interpret the dynamic data from the controller:
[/quote]

Thank you Smile

Ok, so far so good... but this will be available for only the Test controller. If i have another controller, let's say Contact, i have to copy / paste the code

Code:
$data['page_title'] = 'Welcome To My Site';
$data['footer'] = '&copy; 2007 dotweb';

in it. Ok, so far so good again. But what happens when i need to modify the way that the $data['page_title'] is generated / created ? I need to go trough every Controller and update the code... And that's not what i want... I need a single piece of code that does the header / footer / right/ left job...

For example i have the header in which i'm generating dynamic meta values (keywords etc.) The content of them differs from page to page (controller to controller) but the code that generates them is actually the same... So i need to call that code everytime (with different parameters)... Much like a function...

I need something like

Code:
class Test extends Controller
{
    function Test()
    {
        parent::Controller();
    }

    function index()
    {
        $this->load->header($params = false); //$params if any
        
        $this->load->view('template', $data);

        $this->load->footer($params = false); //$params if any
    }
}

where header() and footer() are different controllers that manages the coresponding views.
#5

[eluser]Michael Wales[/eluser]
I like to extend the controller for things like this. For example:

/application/libraries/GT_Controller.php
Code:
class GT_Controller extends Controller {

    function GT_Controller() {
        parent::Controller();
        $this->load->model('users_m');
        // Get the user's record from the database (FALSE if not logged in)
        $this->data->user = $this->users_m->getById($this->session->userdata('user_id'));
        
        // Get the header and footer, place in the data object, for use in the view
        $this->data->layout->header = $this->load->view('_global/header', $this->data, TRUE);
        $this->data->layout->footer = $this->load->view('_global/footer', $this->data, TRUE);
    }
}

Then, all of my normal controllers are extending the GT_Controller class, so they have access to the user data and header/footer I loaded from the GT_Controller class.

/application/controllers/home.php
Code:
class Home extends GT_Controller {

    function Home() {
        parent::GT_Controller();
    }
    
    function index() {
        $this->load->view('home/home', $this->data);
    }

}

Note: The GT_ prefix in GT_Controller.php is defined in config.php, it defaults to MY_.
#6

[eluser]Matthew Pennell[/eluser]
[quote author="dotweb" date="1192560438"]For example i have the header in which i'm generating dynamic meta values (keywords etc.) The content of them differs from page to page (controller to controller) but the code that generates them is actually the same... So i need to call that code everytime (with different parameters)... Much like a function...[/quote]
Why not use a function then? Put your keyword generator function in a helper, and then either call it in the controller:

Code:
$this->load->helper('keyword');
$data['keywords'] = generate_keywords();

or call it directly in the view:

Code:
&lt;meta name="keywords" content="&lt;?= generate_keywords() ?&gt;" /&gt;
#7

[eluser]John_Betong[/eluser]
Hi DotWeb,

The is way I include common snippets of code in my controller on this site :



&nbsp;

/controllers/jokes.php
Code:
//===========================================
   class Joke extends Controller {

     ...
     ...
     ...

  //===========================================
  function first() {
    $data = $this->m_lib->m_first(NULL);

    $this->j_view($data);
  }

  //===========================================
  function last() {
    $data = $this->m_lib->m_last(NULL);

    $this->j_view($data);
  }

  //===========================================
  function random() {
    $data = $this->m_lib->m_random(NULL);

    $this->j_view($data) ;
  }


  //==============================================
  function j_view($data) {
    $data['htmltitle']      = HTMLTITLE .' - ' .$data['joketitle'];
    $data['pagetitle']      = isset($data['pagetitle'])
                            ? $data['pagetitle']
                            : $data['joketitle'];
        
    $data['search_str']     = $_SESSION['words'];
    $data['search']         = $this->load->view('_search'         , $data, TRUE);

    $data['head_keywords']  = $data['joketitle'];
    $data['j_quotes']       = $this->m_lib->m_quote_random() ;
    $data['dolphin']        = $this->m_lib->m_dolphin();
    $data['znav']           = $this->a_navigate();
    $data['zmenutop']       = $this->a_menutop();
    $data['zleft']          = $this->a_left();
    $data['zlocal']         = $this->a_local();
    $data['header']         = $this->load->view('_doctype'        , $data, TRUE);
    $data['title']          = $this->load->view('_title'          , $data, TRUE);

    $tmp_right              = (LOCALHOST OR isset($data['HOME']))
                            ? '_right'
                            : '_google_120x600';

    $data['right']          = $this->load->view($tmp_right        , $data, TRUE);
    $data['left']           = $this->load->view('_left'           , $data, TRUE);
    $data['joke']           = $this->load->view('_joke'           , $data, TRUE);
    $data['footer']         = $this->load->view('_foot'           , $data, TRUE);
    $data['search']         = $this->load->view('_search'         , $data, TRUE);

    $output                 = $this->load->view('view_001', $data, TRUE);
    echo $output;
  }//endfunc

}//endclass
&nbsp;

Cheers,

John_Betong
&nbsp;
#8

[eluser]dotweb[/eluser]
Ok, thank you guys (and / or girls). Now i have three solution (if my counting is correct Smile ) and the Buddy Bradley's helper is the closest to what i need. I will come back if something will break my brains again Smile




Theme © iAndrew 2016 - Forum software by © MyBB