douxueke5653 2016-01-29 17:56
浏览 44
已采纳

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

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>
  • 写回答

2条回答 默认 最新

  • doubailian4459 2016-01-29 18:02
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么