如何处理MVC网站中的特定Javascript视图

时间:2013-03-18 14:45:35

标签: php javascript codeigniter

我知道这个问题已被问过几次,但我似乎无法找到解决问题的正确方法。

我正在使用CodeIgniter,这是一个遵循MVC(模型 - 视图 - 控制器)原则的PHP框架。

我有很多javascript文件,比如jQuery和其他通用库,我需要包含在 head 标记中的每个页面。我还查看了直接嵌入脚本块视图中的特定javascript。

视图 customerInfo.php

的示例
<script type="text/javascript">
    // some javascript here for customerInfo view
    // I MUST be able to use PHP variables in this script block ex:
    var customerName = "<?php echo $customerName; ?>";
</script>

<p class="someClass">Hello <?=$customerName?>, may the lord provide you with bananas</p>

我的网站布局如下:

<html>
<head>
    <script src="http://coolStorybro.com/jquery.js" type="text/javascript"></script>
    <script src="http://coolStorybro.com/commonStuff.js" type="text/javascript"></script>
</head>

<body>

<div class="header">some header stuff</div>

<div class="pageContent">
   <?php echo $view; ?>   // In this case, $view will hold the view customerInfo
</div>


</body>
</html>

我的目标是让我所有的javascript都在同一个地方,这样我就可以缩小它并最终将它放在我的页面底部以进行加载优化。

我知道我可以将视图特定的javascript存储在名为 customerInfo.js 的单独文件中,并将其加载到 head 标记中,但我觉得它也是为每个视图创建一个单独的javascript文件很多,我将无法在其中使用 PHP ,我必须这样做。

所以我问你们,我的问题是否有解决方案,或者我注定要将我的页面聚集在一起使用脚本块?

感谢您的时间。

修改

有没有办法将视图特定的javascript(所有视图)放在一个文件中,并以某种方式将其分隔在模块中,并且仅为所需视图加载特定模块?

我们说 viewsJS.js 包含所有特定于视图的javascript:

module ("customerInfo", 
    $(".someClass").html("Bla bla bla");
);

module ("invoiceInfo", 
    // Some code to be executed when the invoiceInfo module is loaded
);

一旦定义了每个模块(视图)的javascript,就会有一种方法将它们加载到相应的视图中。问题是,我不能在我的视图中有一个脚本块来“加载”模块,所以..我很无能为力。

1 个答案:

答案 0 :(得分:1)

application/views

中创建此文件夹结构
/includes/javascript.php
/template.php

然后将您的主要代码放在 template.php

<html>
<head>
  <?php echo $this->load->view('includes/javascript'); ?>
</head>

<body>

<div class="header">some header stuff</div>
...

将所有javascript放入 javascript.php

application/views/includes

例如:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>

结局输出在您的浏览器中看起来像:

<html>
<head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
</head>

<body>

<div class="header">some header stuff</div>
...

要加载特定的javascript文件,您可以在javascript视图中执行类似的操作:

<?php if (is_frontpage()) : ?>
   <script src="http://localhost/javascriptforfrontpage.js"></script>
<?php endif; ?>

<?php if (is_contact()) : ?>
   <script src="http://localhost/javscriptforcontactpage.js"></script>
<?php endif; ?>

<?php // this would be loaded everywhere ?>
<script src="http://localhost/javascripteverypage.js"></script>

仅加载客户信息javascript:

首先,您从控制器发送到视图$data['load_js_customer_info'],例如像:

public function customerInfo()
    {

        $data['load_js_customer_info'] = "Customer Info";
        $data['customer'] = $this->mdl_admin->get_customer();
        $this->load->view('template', $data);
    }

并在视图中(包含所有javascript)你创造了一个条件:

 <?php if ($load_js_customer_info) : ?>
   <script src="http://localhost/javscriptforcustomerinfo.js"></script>
 <?php endif; ?>

所以,如果它不是来自客户信息控制器,则不会加载javscript。