如何最好地保持我的控制器视图不可知?
我在控制器中有以下代码:
$this->viewdata->scripts = array(
'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
'jstree/jquery.jstree-1.0-rc2.js',
'jquery.hotkeys-0.8.js',
'tags/index.js'
);
$this->viewdata->styles = array(
'reset.css',
'tags.css'
);
在我看来,我有一个 foreach
将列出的每个 javascript/css 文件包装到相应的 HTML 标记中。我正在使用主模板/布局方法;我只需填充主模板中的任何/所有字段,最后就会得到我的网页。因此,将 javascript/css 全部设置在控制器中对我来说是有好处的,这样我的 和
就可以位于 < code>
然而,我最近意识到 javascript 和 css 不属于控制器,因为渲染的视图可能会发生变化,并且可能有非常不同的 javascript/css 要求。
我基本上只想保留一个主模板以提高可维护性,但也从控制器中删除对 view/javascript/css 的所有引用。有什么可以帮助我做到这一点?
谢谢。
I have the following code in a controller:
$this->viewdata->scripts = array(
'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
'jstree/jquery.jstree-1.0-rc2.js',
'jquery.hotkeys-0.8.js',
'tags/index.js'
);
$this->viewdata->styles = array(
'reset.css',
'tags.css'
);
In my view I have a foreach
to wrap each of the listed javascript/css files into the respective HTML tag. I'm using a master template/layout approach; I just populate any/all fields in the master template and I end up with my web page. So, it's good for me to have my javascript/css all set up in the controller so my <script>
s and <link>
s can be in the <head>
, where they belong.
However, it dawned on me recently that the javascript and css do not belong in the controller because the view that's rendered is subject to change and may have very different javascript/css requirements.
I basically want to keep just one master template for maintainability but to also remove all reference to view/javascript/css from the controllers. What's out there to help me do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不使用 CI,但我经常使用 Zend 和 Symfony。在这两个框架中,都有某种资产助手,您可以从视图中调用以将 css 或 js 文件添加到堆栈中。然后,您可以调用类似的助手来在您的头脑中输出这些文件。例如在 symfony 中:
View:
Layout:
我认为如果不是开箱即用的话,这样的东西在 CI 中实现起来相对容易......
I dont use CI, but i regularly use Zend and Symfony. In both these frameworks there is some sort of asset helper you can call from the view to add css or js files to the stack. You then place a call to similar helper to output those files in your head. for example in symfony:
View:
Layout:
I would think something like this would be relatively easy to implement in CI if not present out of the box...
我有一个项目,我做了同样的事情,尽管不那么雄辩。我想我可以通过“动态”加载 js 和 css 来减少发送到客户端的信息量。我使用控制器定义文件名数组并在视图中循环以包含它们。
我记得只有一个js文件我最终没有包含进来。
您必须决定您更相信以下哪一项。
这是一个混乱的决定,你必须找到自己的方法。
I had a project that I did the same thing, albeit less eloquently. I thought I would reduce the amount of information being sent to the client by "dynamically" loading js and css. I used the controllers to define arrays of file names and loops in my views to include them.
I recall that there was only one js file I didn't end up including all the time.
You have to decide which of the following you believe more than the other.
It's a messy decision you have to just find your own way through.
我意识到您可能没有使用该代码示例中的 cakePHP,但是!他们有一个很好的方法来处理这个问题。它是“内联”样式、脚本等的参数...
在您的标签中,您有以下内容:
在视图文件本身中,您有如下代码:
因此,实际的视图文件呈现,存储所有脚本到某处的某个数组中,然后渲染布局文件(页眉、页脚,等等)。
布局文件获取存储的数组并渲染到 $scripts_for_layout 变量中。
非常天才,而且效果很好。我建议做这样的事情。
参考:http://book.cakephp.org/view/1589/script
I realize you're probably not using cakePHP from that code sample, but! They have a great methodology of handling this problem. It's a parameter for "inline" styles, scripts, etc...
In your tag, you have something such as:
And in the view file itself, you have code like this:
So the actual view file renders, stores all of the scripts into some array somewhere, then renders the layout file (header, footer, what have you).
The layout file takes the arrays that were stored and renders then into the $scripts_for_layout variable.
Pretty genius and works great. I suggest doing something like this.
Reference: http://book.cakephp.org/view/1589/script
您的项目或团队是否真的足够大,足以让 SoC 成为您关注的问题?或者至少,值得花时间问这个问题并等待答案吗?您预计您的视图和 css / js 文件稍后会发生变化吗?
如果您对这些问题的回答是肯定的,并且值得修改您的设计,那么您可以使您的视图包含 css 和 js 本身。会有很多共同点,因此创建仅包含常见 css 和 js 文件的视图,并将该视图文件包含在其他视图中。你在这里不需要任何复杂的东西。或者至少,我不会。
Is your project or team actually large enough for SoC to be a concern? Or at least, one worth the time of asking this question and waiting for an answer? Do you expect your views and css / js files to change sometime later?
If your answer to these is yes, and it is worth modifying your design, then you can make your views include the css and js themselves. There will be a lot in common, so make views that just have the common css and js files in them, and include that view file in your other views. You don't need anything complicated here. Or at least, I wouldn't.
您拥有正确的方法,因为您希望所有全局脚本和样式都位于一个控制器中。您认为您希望能够灵活地将特定脚本和样式包含到特定页面,这是正确的想法。
为此,您可以创建基本控制器和扩展控制器。您的基本控制器将包含所有全局脚本和样式。您的扩展控制器将包含仅特定于特定页面(或页面组)的脚本和样式。
查看以下有关创建基本控制器的链接:
http://philsturgeon.co.uk/blog /2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
请注意,在 CI2 中,基本控制器应位于 /application/core/ 目录中。
Your have the right approach in that you want all your global scripts and styles located within one controller. And you are on the right path in thinking that you want the flexibility of being able to include specific scripts and styles to specific pages.
To achieve this, you can create a base controller and extending controllers. Your base controller will contain all your global scripts and styles. Your extending controllers will contain scripts and styles specific only to a certain page (or group of pages).
Check out the following link on creating a base controller:
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
Note that in CI2 that base controllers should be located in the /application/core/ directory.