doujiaozhao2489 2014-07-17 15:05
浏览 108
已采纳

PHP - 将脚本注入<head>

Run into a bit of a sticky situation which I can't seem to wrap my finger around. Basically what I am trying to achieve is having the ability to inject different Javascript files on different page.

Some simple, random example:

  • Page 1: import jquery.js
  • Page 2: import mootools.js

So what I have done is, I've created a function called addScript() like so:

function addScript($file) {

    $script = '';
    $script .= '<script src="'. REL_PATH . '/path/to/file/' . $file . '">';
    $script .= '</script>';

    return $script;
}

so if I call addScript('jquery.min'); it, outputs correctly.

What I now want to do is replace the closing </head> tag with the output from the above function. If I do the following then it works fine:

ob_start();
require_once("models/header.php");
$contents = ob_get_contents();
ob_end_clean();

echo str_replace('</head>', addScript('jquery.js') . '</head>', $contents);

However I would like this to be a little more dynamic as there may be multiple script that I need to inject on each page like so:

addScript('script.js');
addScript('script2.js');
addScript('script3.js');

I then thought of creating a getHead() function with a foreach loop inside and returning str_replace there instead but this did not work.

Can anyone guide my in the direction to dynamically inject as many script as required and output the last bit of the head?

  • 写回答

3条回答 默认 最新

  • dongxueji2838 2014-07-17 15:13
    关注

    Why not do something like this:

    class Assets {
        private static $css = array();
        private static $js = array();
    
        static function add_style($path) {
            self::$css[] = $path;
        }
    
        static function add_script($path) {
            self::$js[] = $path;
        }
    
        static function get_styles() {
            $output = '';
            foreach(self::$css as $path) {
                $ouput .= '<link rel="stylesheet" href="'. $path .'" />' . "
    ";
            }
            return $ouput;
        }
    
        static function get_scripts() {
            $output = '';
            foreach(self::$js as $path) {
                $ouput .= '<script type="text/javascript" src="'. $path .'"></script>' . "
    ";
            }
            return $ouput;
        }
    }
    

    Then anywhere in your project:

    Assets::add_style('path/to/style.css');
    Assets::add_script('path/to/jquery.js');
    

    And in header.php:

    <head>
        <!-- other header stuff -->
        <?php echo Assets::get_styles(); ?>
        <?php echo Assets::get_scripts(); ?>
    </head>
    

    Is much more convenient, and you can can extend the class to do more fancy stuff.

    Disclaimer: there is much debate about using static vars, as they look like globals. I agree, but this is quick-and-dirty and works no matter what kind of framework you use. You can also make the variables oldschool instance vars, but then you'll have to pass the assets object to the header.php as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看