简体   繁体   English

如何指定Kohana PHP局部视图CSS和JS依赖项

[英]How to specify Kohana PHP Partial view CSS and JS dependencies

I am learning to make use of "Views within views" in Kohana PHP framework in order to write more modular code. 我正在学习利用Kohana PHP框架中的“视图中的视图”来编写更多的模块化代码。 The following code allows me to pass data dependencies to the partial view and then I can render them in a loop. 以下代码使我可以将数据依赖项传递给局部视图,然后可以将其呈现为循环。

foreach ($datas as $data) {
    $view = new View('partial');
    $view->set('data', $data);
    $view->render(TRUE);
}

My question is how do I include JS and CSS dependencies? 我的问题是如何包含JS和CSS依赖项? Ideally I want to put them inside the 'partial' view file ie partial.php 理想情况下,我想将它们放在“部分”视图文件中,即partial.php

partial.php
<link> ... </link>
<script> ... </script>
<div> ... </div>

But this is bad because if I render the partial view in a loop they would get included multiple times. 但这很糟糕,因为如果我在循环中渲染部分视图,它们将被多次包含。 Is there a way to do it with a Kohana helper function of sorts that allows me to append JS and CSS to the bottom of the page and also check for duplicates so if a file is already there, it will not include it again. 有没有一种方法可以使用Kohana辅助函数进行操作,该函数使我可以将JS和CSS附加到页面底部,还可以检查是否存在重复项,因此如果文件已经存在,它将不再包含该文件。

The reason I want to put this inside partial.php is so that other developers using the partial view will be able to easily re-use the code without looking for the JS and CSS dependencies. 我之所以将其放在partial.php中,是为了使其他使用partial view的开发人员能够轻松地重用代码,而无需寻找JS和CSS依赖项。

In my opinion this is not the best practice to render or link a stylsheet inside the code. 我认为这不是在代码中呈现或链接样式表的最佳实践。 You can create an array, called extra_scripts put your dependent css files in, and render it if necessary. 您可以创建一个名为extra_scripts的数组,将依赖的CSS文件放入其中,并在必要时对其进行呈现。

Check this array in your controller's after function 在控制器的after函数中检查此数组

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

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