Welcome Guest, Not a member yet? Register   Sign In
Template Library Version 1.4.1

[eluser]Ben Swinburne[/eluser]
Not sure if this has been noted or if it's even the right place to do so

Line 460 should be

Code:
$js .= "[removed]\n";

rather than

Code:
$js .= '[removed]\n';

The single quotes cause the \n to be taken literally and a \n is output in the template.

[eluser]Ben Swinburne[/eluser]
Anyone have any idea why the template wouldn't render in an extended Core Library?

I'm extending CI_Exceptions with MY_Exceptions and am overriding the show_404() method.

Code:
function show_404($page = '', $log_error = TRUE) {
    $this->ci =& get_instance();
    $this->ci->template->set_master_template( 'template' );
    $this->ci->template->render();
}

But I just get a white screen.

Any help would be appreciated.

Ben

[eluser]WanWizard[/eluser]
A white screen is usually caused by an error while error reporting is disabled.

Most logical cause for the error: the template library isn't loaded yet at that point. You might have to use
Code:
function show_404($page = '', $log_error = TRUE) {
    $template =& load_class('template');
    $template->set_master_template( 'template' );
    $template->render();
}
If that will work depends on what dependencies the Template library has, as nothing will be loaded at this point (the autoload is called from the CI_Controller constructor, which isn't executed in a not-found situation.

[eluser]Ben Swinburne[/eluser]
Tailing the error log shows nothing when it white screens unfortunately.

var_dump( $this->ci->template ); shows that there's an object there which has a couple of regions populated as expected.

The code in your example above causes the following error to be echoed on screen.

Code:
Unable to locate the specified class: template.php

Thanks

Ben

[eluser]WanWizard[/eluser]
If you get that error there is no template.php in APPPATH.'libraries'. But if your template library is loaded, you don't need to do that.

Where do you trigger the show_404()? Is it by CI when you give an invalid URL, or by your code?

[eluser]Ben Swinburne[/eluser]
I'm triggering it in a controller using the show_404() function rather than the method (but if I recall correctly the function calls the method in Exceptions anyway?)

In it's most basic form it's as follows (note: typed here rather than copied from my actual controller so forgive any obvious mistakes unless it'd be the one causing the template problem Smile)

Code:
class Test {
    function __construct() { parent::__construct(); }

    function index() {
        show_404();
    }
}

[eluser]WanWizard[/eluser]
Ok. So in that case all CI libraries are loaded, and it works.

But if you request a URL that can't be routed, the bootstrap will call show_404() directly. If that happens the Exception class (including your extension) is loaded without autoload being executed, in which case you won't have the CI libraries loaded.

A quick check shows that the Template library uses the Loader class to load additional libraries and helpers. This is not available when you're in a "URL can not be routed" situation. So make sure you test that, and deal with any issues.

Back to the issue at hand: the render() method of the Template library will write the output to CI's output class, in the assumption that will be used to process the output and send it to the browser.

In this case you don't want that, you want to echo it out. So use this:
Code:
function show_404($page = '', $log_error = TRUE) {
    $this->ci =& get_instance();
    $this->ci->template->set_master_template( 'template' );
    echo $this->ci->template->render(NULL, TRUE);
    exit;
}

[eluser]Ben Swinburne[/eluser]
Thanks for your help WanWizard- the above (#231) works perfectly!

As you mention, it does indeed behave differently with a real 404 and a forced 404 but easily resolved as you pointed it out. I would have missed that I expect!

Thanks again.

Ben

[eluser]winminsoe[/eluser]
Hi All,
It may be too old to warm up this topic.But i have some issue that i can't figure it out.Assume that i'm setting up master template in admin controller.

Eg.
Code:
$this->template->set_master_template('admin/index');

And it show the right template.
But when i go to home controller.It normally back to default template.Eg.home.php.

Do I have to write each line in every controller that i want to display with admin theme.
Code:
$this->template->set_master_template('admin/index');




[eluser]WanWizard[/eluser]
The best way to go about this is to use base controllers. They are also the perfect location to check for the correct rights.

I usually have a bunch of them, at least for Public, Member and Admin states. These controllers extend CI_Controller, and your controllers in their turn extend one of the base controllers.




Theme © iAndrew 2016 - Forum software by © MyBB