【问题标题】:Accessing a Javascript namespace from a phtml file从 phtml 文件访问 Javascript 命名空间
【发布时间】:2015-01-14 20:45:14
【问题描述】:

我正在使用一个 phtml 文件,该文件需要从托管在同一服务器上的 javscript 文件中读取静态 json 对象。

我在实际访问 JSON 对象时遇到了麻烦,因此我可以对其进行操作并将其显示在屏幕上(最终使用适当的锚点等)

这是我尝试访问对象的方式,但 php 对我来说相对较新。

<?php
require_once '/path/to/my/resource.js';
$json = ns.path.VARIABLE;
?>
<?php echo $json; ?>

还有我的 javascript 文件,位于 /path/to/my/resource.js:

ns.path.VARIABLE = {
    Part1: {
        'Key1': 'Value1',
        'Key2': 'Value2'
    }
    Part2: {
        'Key1': 'Value1',
        'Key2': 'Value2'
    }
}

我没有成功。我可以看出 js 文件已包含在对浏览器的响应中,但我无法访问 VARIABLE Json 对象。

知道如何访问 JSON,然后我将解析并呈现给浏览器吗?

【问题讨论】:

    标签: javascript php json require


    【解决方案1】:

    这是不可能的。 PHP 运行在服务器上,而 JS 运行在客户端。您最好的机会是从文件中删除 ns.path.VARIABLE 部分,并在其上使用 file_get_contents() 将其作为实际 JSON 文件读取

    例子

    resource.json

    {
        Part1: {
            'Key1': 'Value1',
            'Key2': 'Value2'
        }
        Part2: {
            'Key1': 'Value1',
            'Key2': 'Value2'
        }
    }
    

    PHP 代码

    <?php 
    
    $json = json_parse(file_get_contents('resource.json'));
    

    就这么简单!

    编辑

    在您发布的具体代码上,您可以执行以下操作,但似乎并不安全或灵活:

    // Load the file as a string
    $js_string  = file_get_contents('resource.js');
    // Remove the namespace part so that you can parse it as a JSON string
    $js_string = str_replace('ns.path.VARIABLE =', '', $js_string);
    // Convert it to an associative array you can use in PHP
    $array = json_decode($js_string);
    

    【讨论】:

      【解决方案2】:

      这里有一些问题。首先,你所说的 JSON 实际上根本就不是 JSON。通过http://jsonlint.com/ 之类的工具运行它,您会看到。正确的 JSON 应该如下所示:

      {
          "Part1": {
              "Key1": "Value1",
              "Key2": "Value2"
          },
          "Part2": {
              "Key1": "Value1",
              "Key2": "Value2"
          }
      }
      

      另外,您正在混淆 PHP 和 Javascript。正如其他人已经说过的,JS 在浏览器中运行,PHP 在服务器上运行。您不能按照您尝试的方式混合它们。 @Loupax 提供的答案基本上是正确的,但我相信它可以做得更容易。而且它不需要你包含的文件是 JSON,一个普通的 JS 对象就可以了。 (它们是有区别的);像这样的:

      <script type="text/javascript">
         var ns = {
            path: {}
         };
         <?= include '/path/to/my/resource.js'; // this will just paste the contents of that file inside this script block before sending it to the browser ?>;
         // here you can use that object 
         console.log(ns.path.VARIABLE.Part1.Key1); 
      </script>
      

      请注意,我首先必须创建带有 path 对象的 ns 对象,以便能够将 VARIABLE 对象分配给它。这还假设您保留了所谓的 JSON 文件,除了 Part1 和 Part2 之间缺少逗号

      很难从您的代码的 sn-p 中分辨出来,但我认为您可能只需调用它variable 就可以逃脱,然后您就不必做第一步了。还有一点建议,在命名变量时尽量保持一致。普遍的共识是类以大写字母开头,函数和变量以小写字母开头,常量全部大写。您不必遵循这些规则,只需尝试坚持您的选择即可。它将使编码和调试变得更加容易。

      作为结论,我的代码可能看起来像这样:

      文件/to/include.js

      {
          part1: {
              key1: 'Value1',
              key2: 'Value2'
          },
          part2: {
              key1: 'Value1',
              key2: 'Value2'
          }
      }
      

      view.phtml

      <script type="text/javascript">
         var paths = <?= include 'file/to/include.js'; ?>;
         console.log(paths.part1.key1); 
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 2013-03-08
        • 2012-08-06
        • 1970-01-01
        • 2015-12-01
        • 1970-01-01
        相关资源
        最近更新 更多