Welcome Guest, Not a member yet? Register   Sign In
VIEW WITHIN VIEW - CAN'T GET IT TO WORK
#1

[eluser]Barwick[/eluser]
Hey guys,

But of a CI newbie here, so trying to figure out the small sh*t that's probably super simple.

I'm trying to create some clean code and structure for my application. I have a header_view that I've posted below. But instead of layout out all the js and css scripts in this view, I wanted to point to another from the controller - a view within a view. I've read numerous posts on here and couldn't figure it out.

What am I doing wrong? I have a variable in the header_view below called $scripts. In my controller, I'm trying to pass through the scripts_view to the variable in my header...I'm assuming I'm not doing something right in controller (which I've also pasted below).

Note, I don't want to use a template system. Just want an organized application. Smile

header_view.php:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xml:lang="en" lang="en"&gt;
    
&lt;head&gt;
    
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
    &lt;title&gt;&lt;?php echo $title; ?&gt; &lt;/title&gt;
    
    &lt;?php  echo $scripts; ?&gt;
    
&lt;/head&gt;

&lt;body&gt;
    
<div id="wrapper">

Controller:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dashboard extends CI_Controller{
    
    function __construct(){
        parent::__construct();
        $this -> load -> model('user_model');
    }
    
    public function index()
    {
        $session = $this->session->userdata('logged_in');        
        
        if ($session == 1)
        {
            $data['title'] = 'Dashboard';
            $data['scripts'] = $this -> load -> view('scripts_view');
            
            $this -> load -> view('shared/header_view', $data);
            $this -> load -> view('dash', $data);
            $this -> load -> view('shared/footer_view', $data);
        }
        else {
            redirect('register');
        }
    }
    
}

Any help would be greatly appreciated boys and girls! love the framework so far - but still trying to hammer it down!

Cheers,
Mike
#2

[eluser]harshitha[/eluser]
why this way, what I mean is, why you need to use view within view?
#3

[eluser]rochellecanale[/eluser]
Try to use iframe
#4

[eluser]michalsn[/eluser]
Code:
$data['scripts'] = $this -> load -> view('scripts_view', '', true);
#5

[eluser]Barwick[/eluser]
[quote author="harshitha" date="1351057399"]why this way, what I mean is, why you need to use view within view?[/quote]

I suppose I don't need to for this particular incident - I just like things separated and organized. Plus diff scripts will need to be loaded further down the road.

I'll need to embed a view within a view, so I thought I'd ask and learn now. Smile

#6

[eluser]Barwick[/eluser]
[quote author="rochellecanale" date="1351064211"]Try to use iframe[/quote]

Funny guy...everyone loves a forum clown, yeah?
#7

[eluser]Barwick[/eluser]
[quote author="michalsn" date="1351064605"]
Code:
$data['scripts'] = $this -> load -> view('scripts_view', '', true);
[/quote]

I had this originally - and figured this would be the correct function. But nope, still getting an error. Here's the error message...

FYI: Line 9 is &lt;?php echo $scripts; ?&gt; on the view.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: scripts

Filename: shared/header_view.php

Line Number: 9
#8

[eluser]Barwick[/eluser]
[quote author="michalsn" date="1351064605"]
Code:
$data['scripts'] = $this -> load -> view('scripts_view', '', true);
[/quote]

So what I had originally, and what you posted above, is absolutely correct. I'm just an idiot and didn't paste it in all the controllers.

Which brings up a new question - would a helper be a good idea to load this across all controllers? Without having to go in and do this for each?
#9

[eluser]michalsn[/eluser]
If you need it in all your controllers just extend CI_Controller. More info about extending native libraries: http://ellislab.com/codeigniter/user-gui...aries.html
Example:
Code:
class MY_Controller extends CI_Controller
{
    public $data;

    public function __construct()
    {
        parent::__construct();
        $this->data['scripts'] = $this->load->view('scripts_view', '', true);
    }
        
}

In your controllers use then $this->data
Code:
$this->data['title'] = 'Dashboard';
$this->load->view('shared/header_view', $this->data);




Theme © iAndrew 2016 - Forum software by © MyBB