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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号