简体   繁体   中英

how to make a php function wait to execute until the end of the script

I'm trying to create a queue function for javascript files. Basically, this is the way I want it to work: I want to create a function that will take all of the javascripts sent to it and put them in the appropriate place on the page (ie header or footer, and above dependent scripts).

I want to be able to say: Here's a script. Add it to the queue in the order that it should be in. Then, after all the scripts have been queued up, run the function to write them to the page. So far my code looks like this:

$scripts = array();
function enqueue_script($src="", $script_name="", $script_data="", 
 $script_dependencies=array(), $force_header=false, $additional_atts=array()){
    global $scripts;
    //run check for duplicates

    //run check for dependencies already in the variable

    //run checks to see if this script depends on other scripts
    //$scripts array is saved in increments of 10 to allow for up to 
    //9 dependants

    $i = count($scripts);
    $i = ($i*10)+10;
    $scripts[$i]['src'] = $src;
    $scripts[$i]['script_name'] = $script_name;
    $scripts[$i]['script_data'] = $script_data;
    $scripts[$i]['dependencies'] = $script_dependencies;
    $scripts[$i]['force_header'] = $force_header;
    $scripts[$i]['atts'] = $additional_atts;

}
function write_scripts_header() {
    global $scripts;
    $echo = "";
    $atts = "";
    //create script tag for insertion in header
    foreach($scripts as $s){
        if($s['force_header']){
            foreach($s['atts'] as $a => $v){
                $atts .= " {$a}='{$v}'";
            }
            if($s['src']!=""){
                $echo .= "<script src='{$s['src']}'{$atts}></script>\n";
            } else {
                $echo .= "<script{$atts}>{$s['script_data']}</script>\n";
            }
        }
    }
    echo $echo;     
}
function write_scripts_footer() {
    global $scripts;
    $echo = "";
    $atts = "";
    //create script tag for insertion in footer
    foreach($scripts as $s){
         if(!$s['force_header']){
            foreach($s['atts'] as $a => $v){
                $atts .= " {$a}='{$v}'";
            }
            if($s['src']!=""){
                $echo .= "<script src='{$s['src']}'{$atts}></script>\n";
            } else {
                $echo .= "<script{$atts}>{$s['script_data']}</script>\n";
            }
        }
    }
    echo $echo;
}

Then, in the HTML file part:

<html>
<head>
<?php write_scripts_header();?>
</head>
<body>
<?php write_scripts_footer();?>
</body>
</html>

This works fine if the HTML section is loaded last. However if I have an include that happens in the middle of the body tag, and that include needs to enqueue_script() in the header, then the $scripts variable isn't ready yet when the write_scripts_header() function runs.

How can I make write_scripts_header() and write_scripts_footer() wait until all the scripts have been queued up before running? Or....is there a better way to allow for a scripts queue so that I can write all the scripts to the document at the end?

如果它们都在同一个文件中运行,是否可以将主体内容设为变量$ body,然后先加载它,然后将其回显到<body>部分中?

change
<html>
<head>
<?php write_scripts_header();?>
</head>
<body>
<?php write_scripts_footer();?>
</body>
</html>

to

print "<html><head>".write_scripts_header()."</head><body>".write_scripts_footer()."</body></html>";

尝试使用模板引擎,例如Twig ,在准备好脚本数组之后,您将能够输出html。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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