简体   繁体   English

如何在Codeigniter中加载视图

[英]How to load a view in codeigniter

I have sliced the html of my website in multiple files as per the demand of the site structure and saved them all as views in views folder of codeigniter. 我已根据网站结构的需求将网站的html切片为多个文件,并将它们全部保存为codeigniter的views文件夹中的view。

I am loading the same from a common controller as: 我正在从通用控制器加载相同的内容:

$this->load->view($this->config->item('templates_path') . 'header', $this->data);
$this->load->view($this->config->item('templates_path') . 'navbar', $this->data);
$this->load->view($this->config->item('templates_path') . 'main-container', $this->data);
$this->load->view($this->config->item('templates_path') . $sidebar, $this->data);
$this->load->view($this->config->item('templates_path') . 'main-content', $this->data);
$this->load->view($this->config->item('templates_path') . 'breadcrumb', $this->data);
$this->load->view($this->config->item('templates_path') . 'page-content', $this->data);
$this->load->view($this->config->item('templates_path') . 'main-content-end', $this->data);
$this->load->view($this->config->item('templates_path') . 'footer');
$this->load->view($this->config->item('templates_path') . 'main-container-end', $this->data); 
$this->load->view($this->config->item('templates_path') . 'bottom');

Some view files from the above have only few HTML Tags in the file like: View File 上面的一些视图文件在文件中只有几个HTML标记,例如:View File

$this->load->view($this->config->item('templates_path') . 'main-content-end', $this->data);

has only 只有

</div><!-- /.main-container --> 

is there a way in codeigniter to load the html part directly without creating it as a new view file? Codeigniter中有没有一种方法可以直接加载html部分而不将其创建为新的视图文件?

Create a folder in your Views directory and call it something like 'includes' or whatever you want. 在您的Views目录中创建一个文件夹,并将其命名为“ includes”或您想要的名称。
Then you can include these HTML template pieces in other views using the following PHP: 然后,您可以使用以下PHP将这些HTML模板片段包括在其他视图中:

include('/path/to/include/file-to-include.php');


Comments: 评论:
• the path to the file should be relative to the view file •文件的路径应相对于视图文件
• the included file doesn't have to be a .php file, it can be an HTML file if you like (or it can include PHP code if you want it to) •包含的文件不必是.php文件,如果您愿意,它可以是HTML文件(或者,如果需要,它可以包含PHP代码)
• i tested this in my local installation of codeigniter and it worked as expected •我在本地的codeigniter安装程序中对此进行了测试,并且按预期工作

From what I know, creating a view 'file' is required, an html or a php file must be created and included, the load->view part just does it for you, it's exactly like include, only it throws in some CI logic. 据我所知,需要创建一个视图“文件”,必须创建并包含一个html或php文件, load->view部分正是为您完成的,就像include一样,只是它抛出了一些CI逻辑。

What I normally do for my websites is create a function my MY_Controller called 'mainTemplate', (name can vary) 我通常为我的网站做的是在MY_Controller中创建一个名为“ mainTemplate”的函数(名称可能有所不同)

function mainTemplate($views = null, $data)
{
$this->load->view('header',$data);
$this->load->view('breadcrumbs',$data);

//now some logic, if we have a string for a view, we just load the string
if(is_string($views)
$this->load->view($views,$data)

//if it's an array of views, we just iterate over the array and show each view
foreach ($views as $view)
{
    $this->load->view($view,$data);
}  
 //and now the footer
  $this->load->view('footer',$data);  
}

now you can call inside your controllers something like this : 现在您可以在控制器内部调用类似以下内容的代码:

$this->mainTemplate('pageContent',$data);

You can create a new function for each template and include the views you need for each template. 您可以为每个模板创建一个新功能,并包括每个模板所需的视图。 So I have for example a login template, a main page template, a product template etc. 因此,例如,我有一个登录模板,一个主页模板,一个产品模板等。

hope it's closer to what you asked. 希望它更接近您的要求。

Firstly Create a view file and store it in the views folder if the codeigniter. 首先创建一个视图文件,如果有代码点火器,则将其存储在views文件夹中。 syntax to load view files 加载视图文件的语法

$this->load->view('filenane');

file name not require to have extension. 文件名不需要扩展名。 for more detail please prefer codeigniter 有关更多详细信息,请首选codeigniter

make complete design in ur view side then use the following code to load ur view 在ur视图端进行完整的设计,然后使用以下代码加载ur view

$this->load->view('filenane');

dont use extention in filename to load ur view just use ur file name. 不要在文件名中使用扩展名来加载您的视图,只需使用您的文件名即可。

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

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