2013-05-02 59 views
0

我在使用模板顯示視圖時遇到問題。 我的模板看起來是這樣的:CI模板 - 元素顯示在錯誤的位置

<?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); 

任何想法,爲什麼一切搞砸了嗎?

回答

0

兩個這樣做的方法:

Codeigniter views

加載它提前(像你這樣做),並傳遞給其他視圖

還有第三個可選參數,可以改變函數的行爲,使其以字符串形式返回數據,而不是將其發送到瀏覽器。如果您想以某種方式處理數據,這可能很有用。如果將該參數設置爲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'); 
+0

是的,謝謝。 echo $ content正在工作,因爲如果我加載視圖$ this-> load-> view($ content);所有視圖UNDER這行代碼不工作,但上面的所有視圖加載罰款.....奇怪爲什麼... – Merkley 2013-05-03 09:06:35

+0

也許因爲你使用了兩次$ this-> load-> view($ content) ; 一次在控制器中,另一次在視圖中。 (不確定,但可以解釋) – Simon 2013-05-03 12:33:38