Welcome Guest, Not a member yet? Register   Sign In
Can _output function within a controller load views?
#1

[eluser]inktri[/eluser]
Code:
function _output($output)
    {        
        $this->load->view('header_view');
        echo $output;
        $this->load->view('footer_view');
    }

for some reason only the $output (and not the header/footer) is being outputted for the controller. If I were to move the $this->load->view('header_view') function into the controller's constructor, the header loads properly.

Anybody know what's wrong? I've also tried
Code:
echo $this->load->view('header_view');
#2

[eluser]gtech[/eluser]
*** [edit] THIS IS RUBBISH IGNORE i forgot the _ouput function was 'special' [/edit]
#3

[eluser]christian.schorn[/eluser]
The _output function is called after the controller-method is run ("links" in this case) and the final processing is done, so the views will be loaded, but never displayed.

One possibility is, if you have header/footer files without variables to include them:
Code:
function _output($output)
{
  include('/some/header.php');
  echo $output;
  include('/some/footer.php');
}

But it looks to me as if this will break whith caching turned on!

@gtech: what you did, was forcing the _output routine to be called twice, once by the controller and then once again by the framework, with no effect the second time.
#4

[eluser]gtech[/eluser]
@Christian
yep I forgot _output() was a function to render the final output.. so appologies, I have fixed my code below using your suggestions.

Code:
<?php
class Home extends Controller   {
  function Home(){
    parent::Controller();
  }

  function links() {
    $this->output->set_output('heelp');
  }


  function _output($output)
  {
    include('system/application/views/header.php');
    echo $output;
    include('system/application/views/footer.php');
  }  
}
?>
#5

[eluser]christian.schorn[/eluser]
I just tested it ... this will break when caching is turned on.

When a valid cache-file is found, CI will call CI_Output::_display_cache and exit before the controller class is loaded, so there is no chance to call _output.
#6

[eluser]gtech[/eluser]
Yes your right I tested your theory, I dont think there has been a bug logged about this one.. you might want to mention it in the bug forum.. A simple fix might be to put that "caching has to be turned off for the _output function to work" in the documentation.

I have a feeling that inktri might of not called the $this->output->set_output(<WHATEVER>); function and that is why there is no output at all.

[url="http://ellislab.com/forums/viewthread/76960/"]http://ellislab.com/forums/viewthread/76960/[/url] link to same problem about caching with no replys
#7

[eluser]inktri[/eluser]
is there a way to just call a line after every function in a controller? i'll just rely on the constructor for the header, but don't know how to go about loading the footer view
#8

[eluser]gtech[/eluser]
you could create a library:

system/libraries/template.php
Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Template {
    function dotemplate($template, $data)
    {
        $CI =& get_instance();
        $CI->load->view('header');
        $CI->load->view($template, $data);
        $CI->load->view('footer');
    }
}
?&gt;

system/controllers/home.php
Code:
class Home extends Controller
{
   function index()
   {
      // you could always autoload this line check config/autoload.php
      $this->load->library('template');
      $data['output'] = 'test';
      $this->template->dotemplate('viewname',$data);
   }
}

[url="http://ellislab.com/forums/viewthread/78151/"]http://ellislab.com/forums/viewthread/78151/[/url]
#9

[eluser]christian.schorn[/eluser]
Actually I think this is not a bug - if you go through the routes to find the controller, instantiate it (and maybe load a lot of libraries via the controllers constructor) you lose very much of the effectiveness of CIs caching as it is.

In this case it would be enough to make a special method, either in a MY_Controller, or as you need it. Example:
Code:
class MY_Controller extends Controller
{
  var $header_view = 'header';
  var $header_vars = array();

  var $footer_view = 'footer';
  var $footer_vars = array();

  function _pretty_output($view_name, $view_vars = array())
  {
    $this->load->view($this->header_view, $this->header_vars);
    $this->load->view($view_name, $view_vars);
    $this->load->view($this->footer_view, $this->footer_vars);
  }
}
This version limits you to one view per method and you have to call it manually from a controller method, but you get to keep caching. Smile

Then there are hooks ... you could write your own cache_override hook ... but that's a little much just for layout purposes, IMO.

Edit: Or you could do it via a library, as gtech posted, while I was writing this ...
#10

[eluser]gtech[/eluser]
Ive just edited the code above and tested it to ensure it works, Christians method looks like an equally valid approach. so go with whatever you feel comfortable with.




Theme © iAndrew 2016 - Forum software by © MyBB