Welcome Guest, Not a member yet? Register   Sign In
Creating a Layout Handler rather than using each Controler to render the full page.
#1

[eluser]Xeoncross[/eluser]
Well, I am loving the speed and design of the system so far. However, besides the session handling, the fact that there is no master-layout-system kind of gets me.

I am used to Models, Modules, Plugins (or whatever) being called by a main controller and then the output of that Module being returned to the main controller ($content). The main controller then calls a "layout.php" file and places the $content in it and gives the layout a chance to call other functions like get_recent_comments() and random_quote().

This is the way most CMS handle placing the $content of some Model/plugin into the set "site-wide" layout.

Now with Zend and CI I am introduced to the idea that the Controller could/should handle all this.

Code:
<?php

class Page extends Controller {

   function index()
   {
      $data['page_title'] = 'Your title';
      $data['content'] = 'My Page Text';
      $this->load->view('header');
      $this->load->view('menu');
      $this->load->view('content', $data);
      $this->load->view('footer');
   }

}
?>

I don't like the style of setting the layout sections/views within the controller because now my layout is tied to the Controller - i.e. my Controller is becoming a View!!!

What if I delete the "header" view from my design?
What if I add a new "recent_comments" view?

Not only that, but now I have to type all that code over again in EACH controller so that they all render the FULL page.

IMHO, this is just a bad idea. ;-)


So how could a "master layout" be built that received content from the controller but was free from that controller in every other way and allowed other controllers to add content to the layout?

You know, like a CMS that has a dynamic/changing Nav, Sidebar, and Header.
#2

[eluser]Xeoncross[/eluser]
I have found the following code samples and articles, but they just aren't complete enough...
http://codeigniter.com/wiki/layout_library/
http://codeigniter.com/wiki/View_Object/
http://ellislab.com/forums/viewthread/57902/#284919
http://blaze.haughin.com/
#3

[eluser]Asinox[/eluser]
ohh, very nice, i was thinking about it Smile

Thanks
#4

[eluser]Asinox[/eluser]
I was looking the layout for layout libray...and the layout have the part <?=$content_for_layout?>, but what about if i have different "content" for layout..., for example "Main content","comments","Ads","Others", in the same view.... where "main content" will change and others....where we need to put something like <?=$content_for_layout?> ?

im new with CI, and i dont know if i ll clear...but i cant understant 100% the example for layout in this case, because i see just the <?=$content_for_layout?> for "main content" ... somebody will help me about it?

Thanks.

Code:
<html>
<head>
<title><?=$title_for_layout?></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="/css/main.css" type="text/css" />
</head>
<body>
<div id="pagewidth" >
  <div id="header" ><img src="/images/header.jpg" width="700" height="200"></div>
  <div id="wrapper" class="clearfix" >
    <div id="twocols" class="clearfix">
      &lt;?=$content_for_layout?&gt;
    </div>
  </div>
  <div id="footer" > Footer </div>
</div>
&lt;/body&gt;
&lt;/html&gt;
#5

[eluser]Aquillyne[/eluser]
There's a better way of achieving what you want.

Have your controller call 1 master view file. This view file in turn calls sub-views. If you want to change the layout of the whole site, change the sub-views in the master view file.
#6

[eluser]Xeoncross[/eluser]
Ok, I think I know what I want for now.

I want to have my controller do whatever it does and call a view file that will represent the content of the page.

END OF CONTROLLER

Then I want to build a hook (or extent the Controller Class and add a method to run on __destuct()) to catch the output and place it in a $content variable and then call the "layout.php" file that will place the content of that controller into the site layout.

That way my controllers only have to worry about rendering the content and I don't have to do extra view calls to other stuff.

Can anyone give me an example of a hook that could catch the output? or is there a function in the controller class that is called when the page is over?
#7

[eluser]Référencement Google[/eluser]
You may check out what we have done for Linkster (check about the SVN version of the library)
#8

[eluser]Randy Casburn[/eluser]
[quote author="Xeoncross" date="1216965290"]Can anyone give me an example of a hook that could catch the output? or is there a function in the controller class that is called when the page is over?[/quote]

@Xeon - This is from the Docs for the Hooks class. I'm certain you must have seen this, but it does exactly what you were asking.

Quote:# display_override
Overrides the _display() function, used to send the finalized page to the web browser at the end of system execution. This permits you to use your own display methodology. Note that you will need to reference the CI superobject with $this->CI =& get_instance() and then the finalized data will be available by calling $this->CI->output->get_output()

Sorry if this is a silly answer.

Randy
#9

[eluser]Xeoncross[/eluser]
[quote author="Randy Casburn" date="1216970315"]I'm certain you must have seen this[/quote]

yes, actually, that is why I phrased it like that. I was just being lazy and seeing if someone would post something like this before I wrote it myself. Tongue

Code:
$hook['post_controller'][] = array('function' => 'render_layout');

//........

function render_layout() {
    //Get the instance of the object
    $CI =& get_instance();
    
    //Set the content equal to the current output
    $data['content'] = $CI->output->get_output();
    
    //Place the content in the layout
    $output = $CI->load->view('layout', $data, true);
    
    //Set the Full layout as the final content
    $CI->output->set_output($output);    
}


Now, I need to work on this some but one of the benefits of this function is I can set $CI->use_layout = false; in each controller and then on pages I don't want a layout (like an RSS page or Ajax request) I can skip the layout.

What do you think?



P.S. Thanks for pointing that out. You never know, some times we as humans are so stupid we miss stuff like that...
#10

[eluser]Randy Casburn[/eluser]
Sorry, I realize that didn't answer the question...

Hook config file settings...
Code:
$hook['display_override'][] = array(
    'class' => 'catchOutput',
    'function' => 'catchOutput',
    'filename' => 'catchoutput.php',
    'filepath' => 'hooks',
    'params' => array()
);


Hook file...catchoutput.php
Code:
class catchOutput
{
    function catchOutput()
    {
        
        $CI =& get_instance();

        $fred = $CI->output->get_output();
        
        echo str_ireplace('codeigniter', 'Xeon\'s Wonderous world on magic', $fred);
        exit;
    }
}

That is an example of a hook.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB