0

Template loader taken from this question. While trying to use $this->load->view() in a view file it's throwing issues about MY_LOADER unless the return value is specified as true.

Extended template loader

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Loader extends CI_Loader {

    public function __construct() {
        parent::__construct();
    }

    public function template($template_name, $vars = array(), $return = FALSE) {
        if($return) {
            $content  = $this->view('templates/header', $vars, $return);
            $content .= $this->view($template_name, $vars, $return);
            $content .= $this->view('templates/footer', $vars, $return);

            return $content;
        } else { 
            $this->view('templates/header', $vars);
            $this->view($template_name, $vars);
            $this->view('templates/footer', $vars);
        }
    }
}
?>

Basic controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {
    public function index() {
        $this->load->template('welcome_message');
    }
}

welcome_message

<?php echo $this->load->view('account/login'); ?> // Throws error
<?php echo $this->load->view('account/login', array(), true); ?> // No error

If I don't define the return value as true it throws an error Object of class MY_Loader could not be converted to string

Full stacktrace

    A PHP Error was encountered
Severity: 4096
Message: Object of class MY_Loader could not be converted to string
Filename: views/welcome_message.php
Line Number: 71
Code on line 71: <?php echo $this->load->view('account/login'); ?>

Backtrace:

File: application/views/welcome_message.php
Line: 71
Function: _error_handler

File: application/core/MY_Loader.php
Line: 17
Function: view
Code on line 17: $this->view($template_name, $vars);

File: application/controllers/Home.php
Line: 26
Function: template
Code on line 26: $this->load->template('welcome_message');

File: index.php
Line: 316
Function: require_once

This use to work fine in CI.2, what's the cause of the issue?

Community
  • 1
  • 1
Bankzilla
  • 2,086
  • 3
  • 25
  • 52

1 Answers1

1

Try using view's like below with out echo

<?php $this->load->view('account/login'); ?>

Instead of

<?php echo $this->load->view('account/login'); ?>

Codeigniter view guide