CodeIgniter - 容器视图,用于从这些子视图中获取子视图所需的脚本(js)列表

发布于 2024-11-28 05:34:35 字数 316 浏览 0 评论 0原文

我有一个打开容器视图的控制器。然后,容器将页面的构建交给几个视图,包括“html_meta”视图和“top_bar”、“side_bar”、“content”视图。然而,内容包含几个不同的视图,其中太多可以被视为小工具,或包含不同数据、脚本操作的小型 HTML div。

问题: 如果我希望 HTML 标头/元仅包含特定小工具视图所需的脚本,那么小工具视图是否可以将必要的脚本组件发送回容器视图,以便容器视图可以处理它们并将脚本添加到 html 头部?

  • 如果有帮助,我可以包含代码,但现在,我认为这里会变得混乱。但是,如果您需要代码来可视化它,没问题。谢谢!

I have a controller that opens a container view. The container then hands the building of the page to several views including the "html_meta" view and the "top_bar", "side_bar", "content" views. However, the content contains several different views too many of which can be considered gadgets, or small contained html divs with different data, script actions.

Question:
If I want the HTML header/meta to include only the scripts that that the specific gadget views need, is there a way for the gadget views to send the necessary script components back up the line to the container view so that it can process them and add the scripts to the html head?

  • If it helps I can include the code, but for now, I think it gets messy here. However, if you need code to visualize it, no problem. Thanks!

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

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

发布评论

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

评论(1

计㈡愣 2024-12-05 05:34:35

举个例子,我的控制器调用主 container 但我必须将来自另一个视图的内容传递给它

...
$content['content'] = $this->load->view('about/index', $data, true);
...
$this->load->view('layout/container', $content');

然后 layout/container.php 包含几个

...
$this->load->view("layout/html_meta");
...
$this->load->view("layout/top_bar");
...
<div id="content">
  <?php echo $content?>
</div>

这是可能的,但它是不是子视图“通知”“html_meta”包含它需要的 JavaScript,因为您的视图是从主容器渲染到子视图的。

您可以做的是为外部 java 创建一个帮助器脚本。我正在考虑为每个小部件创建一个 javascript 组,例如

function getJSforWidgetOne() {
   $scripts = array();
   $scripts[] = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js";
   $scripts[] = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js";
   return $scripts;
}

然后在控制器函数中您还传递这些脚本,或者可能传递您的构造函数。

$content['custom_js'] = getJSForWidgetOne();
$content['content'] = $this->load->view('about/index', $data, true);
...
$this->load->view('layout/container', $content');

现在,您可以在 layout/container.php 中访问 $custom_js。但它还没有出现在 layout/html_meta 中。因此,我将其分配给一个数组,然后再次将其传递给 layout/html_meta

...
$for_html_meta['custom_js'] = $custom_js;
$this->load->view("layout/html_meta", $for_html_meta);
...
$this->load->view("layout/top_bar");
...
<div id="content">
  <?php echo $content?>
</div>

然后,在您的 layout/html_meta 中,您现在可以使用 $custom_js 插入除常用脚本之外的其他 JavaScript。

As an example my controller calls the main container but I have to pass it my content which is from another view as such

...
$content['content'] = $this->load->view('about/index', $data, true);
...
$this->load->view('layout/container', $content');

Then layout/container.php contains several

...
$this->load->view("layout/html_meta");
...
$this->load->view("layout/top_bar");
...
<div id="content">
  <?php echo $content?>
</div>

It's possible but it's not the subviews "informing" the "html_meta" to include the javascripts that it needs because your views are rendered from the main container down to the sub views.

What you can do is create a helper for your external java scripts. I'm thinking of having a javascript group for each of your widgets such as

function getJSforWidgetOne() {
   $scripts = array();
   $scripts[] = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js";
   $scripts[] = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js";
   return $scripts;
}

Then in your controller function you also pass those scripts, or possible your constructor.

$content['custom_js'] = getJSForWidgetOne();
$content['content'] = $this->load->view('about/index', $data, true);
...
$this->load->view('layout/container', $content');

So now, $custom_js is accessible in your layout/container.php. But it's not yet in layout/html_meta. So I'll assign this to an array, and pass it again to layout/html_meta.

...
$for_html_meta['custom_js'] = $custom_js;
$this->load->view("layout/html_meta", $for_html_meta);
...
$this->load->view("layout/top_bar");
...
<div id="content">
  <?php echo $content?>
</div>

Then in your layout/html_meta, you can now use $custom_js to insert additional javascripts aside from your common scripts.

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