[英]Best method of including views within views in CodeIgniter
我正在啟動一個大型 codeigniter 項目,並想嘗試為內容片段創建一些可重用的“迷你”視圖,例如可能顯示在不同頁面/控制器上的數據循環。
從主控制器的視圖中調用視圖是否更好? 如果是這樣,如何? 或者我應該從控制器調用“迷你視圖”,從而將視圖的代碼傳遞給主視圖?
其他視圖中的視圖稱為嵌套視圖。 在 CodeIgniter 中包含嵌套視圖有兩種方法:
提前加載視圖並傳遞到另一個視圖。 首先把它放在控制器中:
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
?>
然后將<?=$menu?>
放在您希望菜單出現的位置。
首先把它放在控制器中:
<?php
$this->load->view('home');
?>
然后把它放在/application/views/home.php
視圖中:
<?php $this->view('menu'); ?>
<p>Other home content...</p>
關於最佳方法,我更喜歡第一種方法而不是第二種方法,因為使用第一種方法我不必混淆代碼,它不像包含php。 雖然間接兩者相同,但第一種方法比第二種方法更清晰更干凈!
老實說,我更喜歡通過使用模板視圖然后從控制器加載必要的數據來做到這一點,這意味着重復代碼少得多,並且比從視圖加載視圖更好地遵循 DRY 概念。 特別是對於頁眉、頁腳和菜單等內容。
所以我的模板視圖看起來像這樣:
模板.php
$this->load->view('header',$title);
$this->load->view('sidebar',$sidebar_content);
$this->load->view('main_content',$main_content);
$this->load->view('footer');
然后在我的控制器中,我將所需的數據傳遞給模板,如下所示:
$data['title'] = 'Home Page';
$data['sidebar_content']='pages/standard_sidebar';
$data['main_content'] ='pages/my_home_page';
$this->load->view('template',$data);
這樣做有很多好處。 首先是我可以有多個模板,例如我有兩個主要的模板,一個用於沒有側邊欄的全頁面視圖,一個用於帶有側邊欄的頁面,我還調用 if 語句來決定要包含哪個標題,常規的或帶有管理菜單的那個。
是的,我可以在每個主視圖頁面中包含頁眉、側邊欄和頁腳,但這最終會產生大量重復代碼。 例如,如果我希望我的所有頁面都有一些新的東西,一些其他的小片段,會發生什么? 使用模板,我將代碼片段添加到適當的模板中,然后就完成了。 走另一條路,我找到每個頁面並在那里添加代碼段視圖,在我看來,這相當於在頁面中使用 CSS,既浪費又無法最終維護。
方法一
我使用這個方法到我的視圖中,在我想要的地方插入包含視圖
$this->load->view('include/include_view');
方法二
或者在控制器中,您可以加載更多這樣的視圖:
$this->load->view('header_view');
$this->load->view('list_view');
$this->load->view('footer_view');
沒有一種方法比另一種方法更好,這取決於您是否必須傳遞一些數據(在這種情況下使用方法 2)或者您是否想在主視圖的特定部分中包含視圖(在這種情況下最好使用方法 1 )
方法三
通過主視圖將數據傳遞到包含視圖
進入你的控制器:
$data['title'] = "Title";
$this->load->view('main_view',$data);
在你看來
$data2['title'] = $title;
$this->load->view('include/include_view',$data2);
如果您想將整個數據傳遞給包含視圖,您可以這樣做:在您的控制器中:
$data['nestedView']['title'] = 'title';
在你看來
$this->load->view('includes/included_view', $nestedView);
這是在視圖中包含視圖的簡單方法。無需提前加載視圖。只需將視圖路徑傳遞給其他視圖。
在你的控制器中使用這個:
$data['middle'] = 'includeFolder/include_template_view'; //the view you want to include
$this->load->view('main_template_view',$data); //load your main view
在 main_template_view 中,您可以包含其他視圖:
$this->load->view($middle);
在我看來,為了以更有效的方式解決這個問題,我這樣做了:
您創建一個新的助手(在 application/helpers 中),名稱為(es.common_helpers.php,下划線很重要)。 在此文件中,您將所有功能(例如構建 html 片段)放在一起。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function getHead(){
require_once(APPPATH."views/common/head.php");
}
function getScripts(){
require_once(APPPATH."views/common/scripts.php");
}
function getFooter(){
require_once(APPPATH."views/common/footer.php");
}
在您的控制器中,您只調用一個關於 MVC 的視圖,並從您的自定義助手調用函數。
class Hello extends CI_Controller {
public function index(){
$this->load->helper('common');
$this->load->view('index');
}
}
在控制器中
控制器
<?php
public function view($page = NULL)
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
$data['title'] = ucfirst($page); // Capitalize the first letter
// Whoops, we don't have a page for that
show_404();
}
$data= array('');
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['page_layout']='pages/'.$page;
$this->load->view('page_layout', $data);
}
?>
在 Views 文件夾中創建一個名為 page_layout.php 的頁面
page_layout.php
//This is where you set the layout to call any view through a variable called $page_layout declared in the controller//
<?php
$this->load->view('header');
$this->view($page_layout);
$this->load->view('footer');
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.