我在使用模板显示视图时遇到问题。 我的模板如下所示:
<?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结构外部的左上角。这是来自inspect元素窗口的副本。注意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);
为什么一切都搞砸了?
答案 0 :(得分:0)
两种方法:
提前加载(就像您正在做的那样)并传递到另一个视图
第三个可选参数允许您更改函数的行为,以便将数据作为字符串返回,而不是将其发送到浏览器。如果要以某种方式处理数据,这可能很有用。如果将参数设置为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;
}
从内部加载视图&#34;&#34;观点:
<?php
// Controller
$this->load->view('template');
<?php
// Views : /application/views/template.php
$this->view('vchangeinfo');