简体   繁体   English

如何使php函数等待执行直到脚本结束

[英]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. 我正在尝试为JavaScript文件创建队列功能。 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). 基本上,这就是我想要的工作方式:我想创建一个函数,该函数将接收所有发送给它的javascript并将它们放在页面上的适当位置(即页眉或页脚以及相关脚本之上)。

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文件部分中:

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

This works fine if the HTML section is loaded last. 如果HTML部分最后加载,则效果很好。 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. 但是,如果我有一个include发生在body标签的中间,并且包含需要在标头中使用enqueue_script() ,则当write_scripts_header()函数运行时, $scripts变量尚未准备好。

How can I make write_scripts_header() and write_scripts_footer() wait until all the scripts have been queued up before running? 如何让write_scripts_header()write_scripts_footer()等到所有脚本都排队之后再运行? 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。

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

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