代码点火器 - 在另一个控制器/视图中加载控制器及其数据

时间:2016-01-29 17:56:22

标签: php codeigniter templates

我对在另一个控制器/视图中包含控制器/视图感到困惑。我正在做以下事情并得到有趣的渲染问题:

//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>

我得到的问题是$ about_inner最终不会在“滚动容器”内部加载 - 它会在其他所有内容之前加载和渲染,如屏幕截图所示。

在about视图中加载about_inner视图及其所有相关数据的最佳方法是什么?我需要所有内容($this->about_model->get_content())来自about_inner,因为它也可以通过ajax由其他页面加载。

enter image description here

   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>

2 个答案:

答案 0 :(得分:3)

您只需告诉about_inner函数返回数据而不打印数据。这是function view($template, $data, $return);

的第三个参数
$this->load->view('pages/about-inner',$data, true);

如果你需要做或者,只需设置一个布尔标志作为函数的参数

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

然后,当您调用该函数时,您只需将true传递给该函数,以使其返回HTML而不是打印HTML。

$this->about_inner(true)

答案 1 :(得分:0)

如果您的JS文件夹在root中,则必须使用base_url()函数对其进行定义。因此,请更正get_content方法

中的所有路径

实施例

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

我认为您可以保留一个控制器方法并删除一个。(您可以保留about并删除about_inner

相关问题