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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失