简体   繁体   English

如何将内容放置在codeigniter中的正确位置?

[英]How to place content in right place in codeigniter?

I have this code, and when I call the function with mydomain/index.php/blog everything works, but the content ie "index" in this case is being displayed in the top of the page. 我有这段代码,当我用mydomain / index.php / blog调用该函数时,一切正常,但是内容(即本例中的“索引”)显示在页面顶部。 I want it to be displayed in the content area, that i have defined in my css. 我希望它显示在我在CSS中定义的内容区域中。 Whats the problem? 有什么问题?

<?php
    class Blog extends CI_Controller{

        public function __construct(){
            parent::__construct();
        }

        public function index(){

            $this->load->view('template/header');
            echo "index";
            $this->load->view('template/footer');
        }

    }
    ?>

There are two ways to display output to the browser in CodeIgniter. 有两种方法可以在CodeIgniter中向浏览器显示输出。 Using the CI views and just echoing out the data. 使用CI视图并回显数据。 The echo command executes immediately, so that's why it is at the top of the page. echo命令会立即执行,因此这就是页面顶部的原因。 The load->view method is executed within CI in the output library - so it's not executed exactly in order of your echo statement. load->view方法是在输出库中的CI中执行的-因此,它的执行方式并非完全按照echo语句的顺序执行。

I would say to create another view for your content, then call all views like this: 我想说的是为您的内容创建另一个视图,然后像这样调用所有视图:

$data = array('content_html' => 'index');
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');

Your content view could just echo out the content_html variable: 您的内容视图可以只显示出content_html变量:

// views/template/content.php
echo $content_html;

Alternatively, you can control the content in your controller (although, not the best of ideas): 或者,您可以控制控制器中的内容(尽管不是最好的主意):

$header = $this->load->view('template/header', array(), TRUE);
$footer = $this->load->view('template/footer', array(), TRUE);

echo $header;
echo "index";
echo $footer;

Passing TRUE as the third parameter to the load->view method returns the view as a string instead of outputting it to the browser - allowing you to control the output. TRUE作为第三个参数传递给load->view方法,会将视图作为字符串返回,而不是将其输出到浏览器-允许您控制输出。

if you want to display data in your content area 如果要在内容区域中显示数据

<?php
    class Blog extends CI_Controller{

        public function __construct(){
            parent::__construct();
        }

        public function index(){
            $data['content'] ='Your content';   
            $this->load->view('template/header');
            $this->load->view('template/content_template',$data);                
            $this->load->view('template/footer');
        }

    }
    ?>

And create a separate template file for content and paste this code inside your content file. 并为内容创建一个单独的模板文件,并将此代码粘贴到您的内容文件中。

<?php
print $content; 
?>

Also refer this url http://ellislab.com/codeigniter/user-guide/general/views.html 另请参阅此网址http://ellislab.com/codeigniter/user-guide/general/views.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM