简体   繁体   English

在Java中实现PHP常量的最佳方法

[英]Best way to implement PHP constants in Javascript

So I am using Codeignitor and I am trying to figure out the best way to share my constants with my javascript in a neat maintainable way. 因此,我正在使用Codeignitor,并且试图找出以一种可维护的巧妙方式与我的JavaScript共享常量的最佳方法。

1) in the view I could echo out my variables in like my footer (yuuuck!) 2) I could parse a partial view which contains a template for javascript and inject that in my view (maybe?) 3) I could dynamically create a javascript file like myJavascript.js.php and include that in my header. 1)在视图中,我可以像页脚(yuuuck!)那样呼出变量。2)我可以解析包含javascript模板的局部视图,并在视图中注入(也许?)3)我可以动态创建一个像myJavascript.js.php这样的javascript文件,并将其包含在我的标头中。

What's the best maintainable way to implement PHP into JS in a MVC framework? 在MVC框架中将PHP实现到JS的最佳可维护方法是什么?

To keep my variables nicely wrapped I use a JSON object - that way I won't incur in issues with encoding, slashes, having to manually update the JavaScript every variable I add... 为了使变量保持良好的包装状态,我使用了JSON对象-这样,我就不会出现编码,斜杠等问题,而不必手动更新每个添加的JavaScript JavaScript ...

$variables_to_view['js_variables']['var_name'] = $var_name;

then pass it to the view 然后将其传递给视图

php_variables = <?php echo json_encode($js_variables) ?>;
alert(php_variables.var_name);

There doesn't seem to be anything wrong about echoing your variables in the script tag. 在script标签中回显变量似乎没有什么错。 In fact, frameworks like BackboneJS are encouraging you to do so for data you need to pass to your client-side code. 实际上,诸如BackboneJS之类的框架鼓励您这样做,因为您需要将其传递给客户端代码。

You can use short tag like this: 您可以像这样使用短标签:

For Example: You want to use $abc variable in js, then you will need to write this in js 例如:您想在js中使用$ abc变量,那么您将需要在js中编写此变量

var abc = <?=$abc?>;

You can create php file . 您可以创建php文件。 Something like script.js.php?outfor=1; 像script.js.php?outfor = 1之类的东西;

 <?php
  header("Content-type:text/javascript"); //can be application/javascript.
 ?>

 ABC = <?php echo $abc?>
 CBA = <?php echo $cba?>
 BAC = <?php echo $bac?> //and so on.

Some additional info . 一些附加信息。 If you use "var" in function that variable will be visible only in that function and without "var"means global. 如果在函数中使用“ var”,则该变量仅在该函数中可见,而没有“ var”则表示全局。

So. 所以。

function abc() 
{
  var a = 1; //only in abc()
  b=2;  //global
}

I know that in terms of programming skills it's not the best, but finally it's what I use and it's working. 我知道,就编程技能而言,这不是最好的,但最终,这是我所使用的并且可以正常工作。 To make it short: I put all my constants in a xml file and I have this little script that generates two separate files with the same content, but different syntax. 简而言之:我将所有常量都放在一个xml文件中,我有这个小脚本可以生成两个具有相同内容但语法不同的单独文件。 I am just pasting the code with my values. 我只是用我的价值观粘贴代码。 If it's useful for anybody, I'll be very happy to help. 如果对任何人有用,我将非常乐意提供帮助。 The xml is the simplest possible; xml是最简单的方法。 value

<?php
define("GECOXML_PATH","../xml/geco.xml");
define("PHP_GECO_FN","../.includes/geco.php");
define("JS_GECO_FN","../js/geco.js");
echo "********   GECO (GEnerate COnstants files for PHP and JS) **********<br>";
echo "<br>";
echo "         input xml file: ". GECOXML_PATH."<br>";
echo "         output php file: ". PHP_GECO_FN."<br>";
echo "         output js file: ". JS_GECO_FN."<br>";
echo "********************************************************************<br>";

$geco = (object)xmlParse(GECOXML_PATH);
echo "<br>";
echo "<br>";
echo "************ PHP GECO ************* <br>";
echo "<br>";
$PHP =  gecoPHP($geco);
echo "<br>";
echo "<br>";
echo "************** JS GECO ************<br>";
echo "<br>";
$JS = gecoJS($geco);
writeFiles($PHP, $JS);

//****** Functions *********

function xmlParse ($url) {
        $fileContents= file_get_contents($url);
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
        $fileContents = trim(str_replace('"', "'", $fileContents));
        return simplexml_load_string($fileContents);
    }

function writeFiles($PHPcontent, $JScontent)
{
            echo "<br> PhP ok:". file_put_contents(PHP_GECO_FN, $PHPcontent)  . "<br>";
            echo "<br> JS ok:" . file_put_contents(JS_GECO_FN, $JScontent) . "<br>";
}

function gecoPHP($gecoOBJ)
{
    foreach ($gecoOBJ as $key => $value)
    {
        if (is_numeric(str_replace(" ","",$value)))
            {
                $line = "define(\"" . $key . "\",". intval($value) .  ");\n";
            }
            else
            {
                $line = "define(\"" . $key . "\",\"". $value .  "\");\n";
            }
        $phpContent = $phpContent . $line;
     echo $line."<br>";
    }
   return "<?php\n"$phpContent."?>";
}

function gecoJS($gecoOBJ)
{
    foreach ($gecoOBJ as $key => $value)
    {
        if (is_numeric(str_replace(" ","",$value)))
            {
                $line = "var " . $key . "=". $value . ";\n";
            }
            else
            {
                $line = "var " . $key . "=\"". $value . "\";\n";
            }
        $JSContent =  $JSContent . $line;
    echo $line."<br>";
    }
   return $JSContent;
}
?>

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

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