mheavers
mheavers

Reputation: 30208

code igniter - loading a controller and its data within another controller / view

I'm a bit confused on including a controller / view within another controller/view. I'm doing the following and getting funny rendering issues:

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

class Pages extends CI_Controller {

public function about(){ //the standalone about page
    $data['about_inner'] = $this->about_inner();
    $this->load->view('pages/about',$data);
}

public function about_inner(){ //this is separate so it can be loaded by the landing page without the html shell around it
    $this->load->model('about_model');
    $data['about'] = $this->about_model->get_content();
    $this->load->view('pages/about-inner',$data);
}

}

//view about.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
</head>

<body>
<div class="container about" data-page="about">
    <div class="scroll-container about">
        <?=$about_inner; ?>
    </div>
</div>
</body>
</html>

The issue I'm getting is that $about_inner does not end up loading inside of the "scroll-container" - it loads and renders before everything else, as shown in the screenshot.

What's the best way to get the about_inner view and all of its associated data to load within the about view? I need all the content ($this->about_model->get_content()) to come from about_inner since it can also be loaded by other pages via ajax.

enter image description here

SOLUTION

   public function about(){ //the standalone about page
        $data['nav'] = $this->load->view('templates/nav', NULL, TRUE);
        $data['about_inner'] = $this->about_inner(true);
        $this->load->view('pages/about',$data);
    }





    public function about_inner($print =false){ //this is separate so it can be loaded by the landing page without the html shell around it
        $this->load->model('about_model');
        $data['about'] = $this->about_model->get_content();
        return $this->load->view('pages/about-inner',$data, $print);
    }

    //HTML

    //view about:

    <div class="container about" data-page="about">
        <?=$nav; ?>
        <div class="scroll-container about">
            <?=$about_inner; ?>
        </div>
    </div>

Upvotes: 0

Views: 325

Answers (2)

Ohgodwhy
Ohgodwhy

Reputation: 50808

You just need to tell the about_inner function to return the data and not print the data. That's the 3rd argument to function view($template, $data, $return);

$this->load->view('pages/about-inner',$data, true);

If you need to do either or, just set a boolean flag as the argument to the function

function about_inner($print = false){
    ...
    $this->load->view('pages/about-inner', $data, $print);
}

Then when you call the function you can simply pass true to the function in order to get it to return the HTML instead of printing the HTML.

$this->about_inner(true)

Upvotes: 3

Abdulla Nilam
Abdulla Nilam

Reputation: 38670

if your JS folder is in root you have to define it with base_url() function. So correct all the path in get_content method

Ex

<script src ="<?php echo base_url() ?>path/to/your/folder/aaa.js" .....

And i think you can keep one controller method and remove one.(You can keep about and remove about_inner)

Upvotes: 0

Related Questions