开发者

CodeIgniter - container view to get list of scripts (js) needed by sub-views from those subs

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开发者_StackOverflow社区_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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜