Welcome Guest, Not a member yet? Register   Sign In
How can I get show_error() to load views?
#1

[eluser]cdechery[/eluser]
I have this website where all my pages have header and footer, which are views. The head is specially important as it loads all the CSS, the Javascript and formats everything. So you would have in most Controllers methods something like this:

Code:
$this->load->view('head');
$this->load->view('whatever-body');
$this->load->view('foot');

But show_error uses its own HTML template without all the stuff I need, that is already loaded in the head view. I don't want to replicate this.

Can I override show_error() somehow to be able to put the error message inside head and foot?
#2

[eluser]CroNiX[/eluser]
They changed this behavior in the development branch (CI 3) to just use straight views, but that doesn't help you here.

What I do is create a library that brings in the CI object and use my own method for displaying errors. So you could use anything here that you normally use in CI, like db, views, etc.

Something like
/application/libraries/Template.php
Code:
class Template {
  protected $ci;  //variable to hold CI's global object instance

  public function __construct()
  {
    $this->ci =& get_instance();  //retrieve and assign CI's global object
  }

  public function show_message($message = '')
  {
    $head = $this->ci->load->view('head', null, TRUE);
    $foot = $this->ci->load->view('foot', null, TRUE);
    $this->ci->output->set_output($head . $message . $foot); //need to use CI's output if you want to be able to use CI's caching
  }
}

Code:
//(controller)if error
$this->load->library('template');
$this->template->show_message('An error occurred');

You might consider doing something similar in general for your views, so you don't have to "include" the header/footer in every method. Each of my controllers just sends the "content" view to the template, which inserts it in between the header/footer.

Using the same method in the library, it would be something like:
(controller)
Code:
$data['h1'] = 'My H1 Tag';  //set some data for our main content view
$main_view = $this->load->view('some_content', $data, TRUE);  //get the contents of that view as a variable
$this->template->show_message($main_view);  //send the html for this view to the library, which will insert it in between the header/footer.

Now all of your regular methods don't have to manually load the header/footer on every single controller, keeping it DRY Smile And you can autoload your template library so you don't have to load that in every controller, either. I have a different method that generates content for the admin pages the same way, but loading different header/footer.
#3

[eluser]cdechery[/eluser]
Thanks for the reply.

I thought about this solution too of using my own method, maybe in a helper, to be able to call views. But that wouldn't only be enough for MY own generated errors. Whanever CI itself throws an exception it would call it's core show_error() and mess up my whole layout.

As for the the second suggestion of wraping all view loading in a simple method that already includes head and foot, I tried it too, but the thing is, my head view is highly customizable for each situation and it receives some parametrs, so I didn' really give it a go, but maybe I will. Smile
#4

[eluser]Tpojka[/eluser]
@CroNiX
Great, thank you. Still haven't been using template in CI. Looks like it's worth to get to.
Can you explain what is second parameter in:
Code:
$head = $this->ci->load->view('head', null, TRUE);
?
I could see in manual there is an empty parameter which should be as same as these null value.
Is that location of data array we are passing to view file when it is output in browser directly?

Thanks again.
#5

[eluser]CroNiX[/eluser]
@Tpojka, 2nd parameter is null because I'm not passing any data to the "head" view (in this very brief, bare bones example), but I needed to have true in the 3rd parameter to return the view instead of output it so had to enter something there. Maybe it should have just been an empty array().

@cdechery, yes, this is bare bones to show the concept. My actual template library is loading many additional things as well, such as passing an array of sidebar widget view names which the template loads dynamically, if any exist, or it creates the default widgets. The header view shows a login form, or the current logged in users details, link to profile, etc.

Also, you might be able to directly include(/path/to/head.php) within your stock CI error templates instead of using CI to load it. It's just HTML along with some, probably, simple php. I don't use (or ever see) any of those templates except the 404 error once it's out of development mode.




Theme © iAndrew 2016 - Forum software by © MyBB