掌握 CodeIgniter - 模板化/加载视图

发布于 2024-10-01 18:18:03 字数 500 浏览 2 评论 0原文

尝试学习 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');

这是有道理的,但我实际上会在每个控制器(页面)上都有它吗?不确定是否有办法将其包含在初始控制器中(欢迎),然后在其他控制器中以某种方式引用它?或者也许有一些我完全想念的东西

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间你老了 2024-10-08 18:18:03

您可以从视图文件中加载视图。例如,考虑一个名为 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 的功能来加载更动态的区域,将视图作为控制器中的变量返回:

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

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

您的 sidenav 部分可能是它自己的视图文件 sidenav.php,其中您的“块”被硬编码或加载,类似于上面的示例。

我已经用两种方法完成了这件事,包括每个控制器方法中的每一个令人讨厌的视图,以及通过使用加载子视图和动态区域的页面模板,到目前为止,第二种方法让我更高兴。

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.

我不咬妳我踢妳 2024-10-08 18:18:03

从视图内部加载视图可能会导致混乱。

扩展 Controller 类隐藏了该方法带来的大部分复杂性,但仍然利用了通过在每次页面加载时渲染一次来生成公共视图(页脚、页眉、导航栏等)的想法。

具体来说,请参阅 CI 用户指南和 wiki 以获取对 MY_Controller 的引用 - 您可以通过在 ./libraries 目录中创建 MY_Controller.php 文件来扩展它。

在那里,您可以调用视图片段,还可以利用 load->view() 调用的第三个参数=true 功能。您将它们加载到 $this->data 中 - 例如将页脚加载到 $this->data['footer'] 中。在各种控制器中,继续将视图数据添加到 $this->data。在你看来 - 我通常使用一个模板,除了框架 HTML 和一些基本 CSS 之外,几乎不做任何事情,然后将整个页眉、页脚、导航和主要内容块作为从 $this->data

额外的好处 - 如果您是 CI 新手,您可能很快就会寻找如何做 MY_Controller 能让您轻松完成的其他事情:)

我有一个关于简化生成和显示的 wiki 页面查看部分内容,正如您尝试在此处执行的那样,使用 MY_Controller 位于:

https://github.com/EllisLab/CodeIgniter/wiki/Header-and-Footer-and-Menu-on-every-page---jedd

Loading views from within views can lead to confusion.

Extending the Controller class hides much of the complexity that comes from that approach, but still utilises the idea of generating common views (footer, header, navigation bars, etc) by rendering them once on every page load.

Specifically, consult the CI User Guide and wiki for references to MY_Controller - you extend this by creating a MY_Controller.php file in the ./libraries directory.

In there you can call view fragments, also utilising the third-parameter=true feature of the load->view() call. You load these into $this->data - for example loading the footer into $this->data['footer']. In your various controllers, continue adding view data to $this->data. In your views - I typically use a template that does little other than skeleton HTML and some basic CSS, and then echos the entire header, footer, nav and main content lumps as variables taken from $this->data

Added bonus - if you're new to CI, you'll likely soon be looking for how to do other things that a MY_Controller will make easy for you :)

I've got a wiki page on simplifying the generation and display of view partials, as you're trying to do here, using MY_Controller at:

https://github.com/EllisLab/CodeIgniter/wiki/Header-and-Footer-and-Menu-on-every-page---jedd

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文