在代码点火器中创建块/模块 [英] Creating Block/Modules in Code Igniter

查看:104
本文介绍了在代码点火器中创建块/模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在使用CodeIgniter创建一些简单的网络应用程序(真的只是学习框架),但我不能找出一个简单的方法来创建一个块/模块。



我想有一个最近的图像块,我可以放在网站上的任何页面,没有重复在每个页面的控制器中的查询,并将其传递给模板与$ vars。



2个问题:



  1. 视图中的偏移已经,但是如何写一个
    部分,从控制器
    拉出除了
    url指定的。

  2. 如何干净地创建一个
    控制器,为
    提取数据,阻止它并且不为其创建一个

    页面。 / li>

这似乎应该很简单,我只是没有运气找到合适的解决方案。提前感谢。

解决方案

好的,这里是我对widgets,modules,partial views想要打电话给他们。无论哪种方式,我们都指的是页面的可重复使用的项目。



有很多不同的方式来做这个,所以我的方式可能不适合你 - 您正在寻找!



首先,我将窗口小部件作为视图,让我们说路径将是:views / widgets / myview.php



我决定我计划在控制器中使用什么小部件。我的方法是把我想要的视图的路径在一个数组,如:

  $ data ['widgets'] = array ('widgets / myview','widgets / myview2');然后,当我加载我的主视图(仍然在我的控制器),我这样做:

  $ this-> load-> view('main_view',$ data); 

现在widgets数组被传递给主视图。



然后在我的主视图中,当我到达小部件去的地方,我循环通过我的数组,如下:

  foreach($ widgets as $ widget)
{
$ this-> load-> view($ widget);
}

此循环在我的主视图中运行。方便地,您传递到主视图的所有数据都将在您加载的嵌套视图中可用。



我在Codeigniter中创建了许多小部件,这是方法我也在我的CMS中使用。


So far I've been enjoying using CodeIgniter to create some simple web apps (really just learning the framework), but I can't figure out an easy way to create a block/module.

I would like to have have "Recent Images" block that I can drop on any page on the site without duplicating the query in each page's controller and passing it to the template with $vars.

2 questions:

  1. I am using partials in the views already, but how do I write a partial that pulls from a controller other than the one specified by the url.
  2. How can I cleanly create a controller that pulls the data for that block and does not create a
    page for it.

This seems like it should be simple enough, I'm just not having any luck finding the proper solution on own. Thanks in advance.

解决方案

Okay, here's my take on "widgets", "modules", "partial views" or whatever you want to call them. Either way we mean reusable items for pages.

There are a bunch of different ways to do this so my way may not be for you - then again it might be just what you're looking for!

Firstly, I make the widgets as views, let's say the paths would be like: views/widgets/myview.php

I make the decision of what widgets I'm planning to use in my controller. My method is to put the paths to the views I want in an array like:

$data['widgets'] = array('widgets/myview', 'widgets/myview2');

Then when I load my main view (still in my controller) I do this:

$this->load->view('main_view', $data);

Now the widgets array is passed to the main view.

Then in my main view when I get to the place where the widgets go, I loop through my array like this:

foreach ($widgets as $widget)
{
    $this->load->view($widget);
}

This loop is run in my main view. Conveniently, all the data you passed to the main view will be available in the nested views you load.

I've made many widgets in Codeigniter this way and it's the method I use in my CMS's as well.

这篇关于在代码点火器中创建块/模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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