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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度