0

我在使用模板显示视图时遇到问题。我的模板如下所示:

<?php
$this->load->view('includes/header');

if($this->session->userdata('is_loggedin')!=1) //check if logged in 
{
    $this->load->view('includes/not_loggedin');  //includes this when not logged in
} 
else //includes all this when is logged in
{
     if(isset($content)) //check if content variable isnt empty
     {
         $this->load->view($content); //THIS IS MY CONTENT WHIC IS DISPLAYED IN WRONG POS
     }
    $this->load->view('includes/is_loggedin'); //
}

$this->load->view('includes/footer');
?>

错误的位置是指我的表单显示在 HTML 结构之外的左上角。这是检查元素窗口的副本。注意 div 的位置;头部标签为空;并且,头部信息在正文中。

<html lang="en">
<head></head> //head tags are empty
<body>
<div id="settings">...</div>  //this is my loaded div 
<meta charset="utf-8">
<title>Sludinājumu lapa</title> //head info outside tags
<link href="/assets/css/style.css" type="text/css" rel="stylesheet">
<div id="wrapper">....</div>  //i need settings div inside wrapper div 
</body>
</html>

如果不加载该视图,HTML 结构就可以了。将 is_loggedin 和 not_logged 加载到视图中也没有问题。

我的标题包含:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sludinājumu lapa</title>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
</head>
<body>
<div id="wrapper">

页脚包含:

<foter>
<p>developed by kriss</p>
</foter>
</div> //End of "wrapper" div
</body>
</html>

从控制器我传递这样的数据:

    $data['content'] = $this->load->view('vchangeinfo');
    $this->load->view('template', $data);

任何想法为什么一切都如此混乱?

4

1 回答 1

0

这样做的两种方法:

Codeigniter 视图

提前加载它(就像你正在做的那样)并传递给另一个视图

还有第三个可选参数可让您更改函数的行为,以便它以字符串形式返回数据,而不是将其发送到浏览器。如果您想以某种方式处理数据,这可能很有用。如果将参数设置为 true(布尔值),它将返回数据。默认行为是 false,将其发送到您的浏览器。如果要返回数据,请记住将其分配给变量:

// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['content'] = $this->load->view('vchangeinfo', NULL, TRUE);
$this->load->view ('template', $data);

// View

if(isset($content)) //check if content variable isnt empty
{
    $this->load->view($content);
    // OR don't know wich one works.
    // echo $content;
}

“从内部”加载视图:

<?php
// Controller
$this->load->view('template');

<?php
// Views :  /application/views/template.php
$this->view('vchangeinfo');
于 2013-05-02T21:54:24.390 回答