【问题标题】:Cannot destroy active lambda function, nested output buffer wordpress scss compiler无法破坏活动的 lambda 函数,嵌套输出缓冲区 wordpress scss 编译器
【发布时间】:2021-12-03 11:14:08
【问题描述】:

我正在使用 wordpress 并使用操作“template_redirect”在发送到浏览器之前启动 html 内容的输出缓冲区。

我已经导入了一个库,可以在 php 中将 SCSS 转换为 css 代码 https://scssphp.github.io/scssphp

这里是代码示例

$compiler = new ScssPhp\ScssPhp\Compiler();

add_action( 'template_redirect', 'replace_scss', -100 );
function replace_scss(){
    ob_start( function( $buffer ) {
        $scss = 'body { p { color: red; } }';
            $css = $compiler->compile($scss);
            if(!empty($css) && is_string($css)) return preg_replace('scss_compiled', $css, $buffer );
            else return $buffer;
    }); 
}

编辑:// 正常输出时上面的以下函数

body p { color: red; }

但是我收到以下错误。

PHP Fatal error:  Cannot destroy active lambda function in scss_compiler\src\Formatter.php on line 287

我假设它是因为在 formatter.php 中它提到了 ob_start; 当 ob_start 已经在回调中时,你不能启动它。

我想知道有没有其他方法可以获取 html 的内容,进行更改然后返回浏览器?

【问题讨论】:

    标签: php wordpress sass output-buffering ob-start


    【解决方案1】:

    我自己解决了。

    //from the formatter.php in scss php
    public function format(OutputBlock $block, SourceMapGenerator $sourceMapGenerator = null)
      {
          $this->sourceMapGenerator = null;
    
          if ($sourceMapGenerator) {
              $this->currentLine        = 1;
              $this->currentColumn      = 0;
              $this->sourceMapGenerator = $sourceMapGenerator;
          }
    
          $this->testEmptyChildren($block);
    
          //ob_start();
    
          $this->block($block);
    
          $out = ob_get_contents();
    
          return $out;
      }

    我注释掉了ob_start(); 并更换 $out = ob_get_clean();$out = ob_get_contents();

    至于代码

    add_action( 'template_redirect', 'scss_compiler', -100 );
    function scss_compiler(){
        ob_start('scss');
        ob_end_flush();
    }
    
    function scss($buffer){
        $scss = 'body { p { color: red; } }';
        $compiler = new ScssPhp\ScssPhp\Compiler();
        $css = $compiler->compileString($scss)->getCss();
        error_log($css);
        return $buffer;
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      相关资源
      最近更新 更多