获取CodeIgniter的悬挂 - 模板/加载视图 [英] Getting the hang of CodeIgniter - Templating / loading views

查看:146
本文介绍了获取CodeIgniter的悬挂 - 模板/加载视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试学习CI并浏览文档以获得更好的理解。没有得到单独的库,我可以通过包括如下的视图列表来创建一个模板:

  $ this-> load - > view('header'); 
$ this-> load-> view('navigation');
$ this-> load-> view('sidenav_open');
$ this-> load-> view('blocks / userinfo');
$ this-> load-> view('blocks / stats');
$ this-> load-> view('sidenav_close');
$ this-> load-> view('content',$ data);
$ this-> load-> view('footer');

这很有意义,但是我真的会在每个控制器不确定是否有一种方法来包含这个在初始控制器(欢迎),然后在其他人不知何故引用它?

解决方案

您可以从视图文件中加载视图。例如,考虑一个名为 page_template.php 的通用页面模板:

  < html> 
< body>
< div id =header>
<?php $ this-> load-> view('header');?>
<?php $ this-> load-> veiw('navigation');?>
< / div>
< div id =sidenav>
<?php $ this-> load-> view('sidenav');?>
< / div>
< div id =content>
<?php echo $ content;?>
< / div>

< div id =footer>
<?php $ this-> load-> view('footer');?>
< / body>
< / html>

通过使用codeigniter的abilty在控制器中将视图作为变量返回来加载更多动态区域:

  $ template ['content'] = $ this-> load-> view('content',$ data ,真正); 
$ it-> load-> view('page_template',$ template);



通过向加载函数传递TRUE,CI将从视图返回数据,而不是输出到



您的sidenav部分可以是自己的视图文件,sidenav.php,你有你的'块'硬编码或加载类似于上面的例子。 / p>

我这样做了两种方式,包括每个控制器方法中的每个臭味的视图,并通过使用页面模板加载子视图和动态区域,远,第二种方法让我更开心。


Attempting to learn CI and going through the docs to get a better understanding. Without getting a separate library, I could make a template by including a list of views like so:

$this->load->view('header');
$this->load->view('navigation');
$this->load->view('sidenav_open');
$this->load->view('blocks/userinfo');
$this->load->view('blocks/stats');
$this->load->view('sidenav_close');
$this->load->view('content',$data);
$this->load->view('footer');

This makes sense but would I actually have that on each of my controllers (pages)? Not sure if there is a way to include this in the initial controller (welcome) and then in the others somehow reference it? Or perhaps there is something I am missing completely

解决方案

You can load views from within a view file. for example, consider a generic page template called page_template.php:

<html>
<body>
  <div id = "header">
      <?php $this->load->view('header');?>
      <?php $this->load->veiw('navigation');?>
  </div>
  <div id = "sidenav">
     <?php $this->load->view('sidenav');?>
  </div>
  <div id = "content">
     <?php echo $content;?>
   </div>

  <div id = "footer">
      <?php $this->load->view('footer');?>
 </body>
 </html>

Load your more dynamic areas by making use of codeigniter's abiltiy to return a view as a variable in your controller:

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

By passing TRUE to the load function, CI will return the data from the view rather than output to the screen.

Your sidenav section could be it's own view file, sidenav.php, where you have your 'blocks' hard-coded or loaded similar to the above example.

I've done it both ways, including every stinking bit of views in each controller method, and by using a page template that loads sub-views and dynamic areas, and by far, the second method makes me happier.

这篇关于获取CodeIgniter的悬挂 - 模板/加载视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆