【问题标题】:how to make a php function wait to execute until the end of the script如何使php函数等待执行直到脚本结束
【发布时间】:2012-04-12 21:35:38
【问题描述】:

我正在尝试为 javascript 文件创建队列函数。基本上,这是我希望它工作的方式: 我想创建一个函数,它将所有发送给它的 javascripts 放在页面上的适当位置(即页眉或页脚,以及依赖脚本之上)。

我想说: 这是一个脚本。按照它应该在的顺序将它添加到队列中。然后,在所有脚本都排队后,运行函数将它们写入页面。 到目前为止,我的代码如下所示:

$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;
}

然后,在 HTML 文件部分:

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

如果 HTML 部分最后加载,这将正常工作。但是,如果我有一个发生在正文标记中间的包含,并且该包含需要在标头中包含 enqueue_script(),那么当 write_scripts_header() 函数运行时,$scripts 变量还没有准备好。

如何让write_scripts_header()write_scripts_footer() 等到所有脚本都排好队后再运行?或者....有没有更好的方法来允许脚本队列,以便我可以在最后将所有脚本写入文档?

【问题讨论】:

    标签: php javascript queue


    【解决方案1】:

    如果都是在同一个文件中运行,能否将body内容设为变量$body,先加载,再回显到&lt;body&gt;部分?

    【讨论】:

      【解决方案2】:
      change
      <html>
      <head>
      <?php write_scripts_header();?>
      </head>
      <body>
      <?php write_scripts_footer();?>
      </body>
      </html>
      

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2015-12-14
          • 2023-03-21
          • 1970-01-01
          • 2011-03-03
          • 1970-01-01
          • 2022-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多