简体   繁体   English

使用php函数打印动态js文件的列表

[英]print list of dynamic js file using php function

I have this code for printing js files using php function in my index page: 我在我的索引页中有以下代码用于使用php函数打印js文件:

<? PHP
    function admin_js($js, $file = '') {
        global $_admin_js;
        if ($file != '') {
            $file = preg_replace('/\\\/', '/', dirname($file));
        }

        $_admin_js[] = array(
            $js,
            $file
        );
    }

    $_admin_js = array();
    global $_admin_js;
    $value = '';

    foreach($_admin_js as $js) {
        $relative_path = RELATIVE_PATH;
        echo '<script src="' . $relative_path . '/' . preg_replace('/\\\/', '/', $js[0]) . '"></script>' . " \n\t";
    }

    global $_admin_js;
    admin_js('admin/templates/js/jquery-1.10.1.min.js');
    admin_js('admin/templates/js/bootstrap.min.js');
?>

But in action php output is empty. 但实际上,php输出为空。 whats my problem ? 我的问题是什么? how do print ?! 如何打印?

DEMO HERE 此处演示

Your code sequence is wrong. 您的代码顺序错误。

<? PHP
 function admin_js($js, $file = '')
 {
     global $_admin_js;
     if ($file != '')
     {
        $file = preg_replace('/\\\/', '/', dirname($file));
     }

     $_admin_js[] = array( $js, $file);
}

/* here should be the code of declaration */

$_admin_js = array();
global $_admin_js;
$value = '';

// Call this first to populate the array of $_admin_js

admin_js('admin/templates/js/jquery-1.10.1.min.js');
admin_js('admin/templates/js/bootstrap.min.js');

//Now you get the filled up array...

foreach($_admin_js as $js)
{
     $relative_path = RELATIVE_PATH;
     echo '<script src="' . $relative_path . '/' . preg_replace('/\\\/', '/', $js[0]) . '"></script>' . " \n\t";
}

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

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