2017-05-29 188 views
0

如何堆棧輸出緩衝區,以便每個緩衝區都有自己的ID?PHP Scoped輸出緩衝區,如何?

所以,我有一個緩衝區的HTML頁面,但也有一個頁面內容連接a hook。鉤子本身也包含輸出緩衝:

<?php ob_start(); ?> 
<html> 
    <head></head> 
    <body> 
    <?php ob_start(); ?> 
    <?php echo $page->content; ?> 
    <?php echo ob_get_clean(); ?> 
    </body> 
</html> 
<?php ob_get_clean(); ?> 

第一ob_get_clean()剛剛停止輸出緩衝區中的HTML頁面。

那麼,是否有可能有這樣的機制?

<?php ob_start('#template'); ?> 
<html> 
    <head></head> 
    <body> 
    <?php ob_start('#page'); ?> 
    <?php echo $page->content; ?> 
    <?php echo ob_get_clean('#page'); ?> 
    </body> 
</html> 
<?php ob_get_clean('#template'); ?> 

讓每個ob_get_clean()都會有自己的價值:

  • #template<html> … </html>
  • #page<p>Lorem ipsum dolor sit amet.</p>

我得到了這些錯誤:

[28-May-2017 09:17:01 Asia/Jakarta] PHP Fatal error: Cannot destroy active lambda function in G:\server\www\lot\extend\plugin\lot\worker\block-e\index.php on line 5 

[28-May-2017 09:17:01 Asia/Jakarta] PHP Stack trace: 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 1. {main}() G:\server\www\index.php:0 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 2. require() G:\server\www\index.php:48 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 3. Hook::fire() G:\server\www\engine\fire.php:117 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 4. call_user_func_array:{G:\server\www\engine\kernel\hook.php:79}() G:\server\www\engine\kernel\hook.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 5. do_fire() G:\server\www\engine\kernel\hook.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 6. Route::fire() G:\server\www\engine\fire.php:113 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 7. call_user_func_array:{G:\server\www\engine\kernel\route.php:140}() G:\server\www\engine\kernel\route.php:140 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 8. {closure:G:\server\www\lot\extend\page\index.php:46-168}() G:\server\www\engine\kernel\route.php:140 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 9. Shield::attach() G:\server\www\lot\extend\page\index.php:166 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 10. ob_end_flush() G:\server\www\engine\kernel\shield.php:90 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 11. Shield::{closure:G:\server\www\engine\kernel\shield.php:84-88}() G:\server\www\engine\kernel\shield.php:90 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 12. Hook::NS() G:\server\www\engine\kernel\shield.php:86 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 13. call_user_func_array:{G:\server\www\engine\kernel\hook.php:94}() G:\server\www\engine\kernel\hook.php:94 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 14. Hook::fire() G:\server\www\engine\kernel\hook.php:94 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 15. call_user_func_array:{G:\server\www\engine\kernel\hook.php:79}() G:\server\www\engine\kernel\hook.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 16. fn_twitter_cards_replace() G:\server\www\engine\kernel\hook.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 17. Page->__get() G:\server\www\engine\kernel\hook.php:8 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 18. Hook::NS() G:\server\www\engine\kernel\page.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 19. call_user_func_array:{G:\server\www\engine\kernel\hook.php:94}() G:\server\www\engine\kernel\hook.php:94 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 20. Hook::fire() G:\server\www\engine\kernel\hook.php:94 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 21. call_user_func_array:{G:\server\www\engine\kernel\hook.php:79}() G:\server\www\engine\kernel\hook.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 22. fn_image_set() G:\server\www\engine\kernel\hook.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 23. Page->get() G:\server\www\lot\extend\plugin\lot\worker\image\index__.php:7 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 24. Page->content() G:\server\www\engine\kernel\page.php:170 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 25. Page->__call() G:\server\www\engine\kernel\page.php:170 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 26. Page->__get() G:\server\www\engine\kernel\page.php:42 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 27. Hook::NS() G:\server\www\engine\kernel\page.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 28. call_user_func_array:{G:\server\www\engine\kernel\hook.php:94}() G:\server\www\engine\kernel\hook.php:94 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 29. Hook::fire() G:\server\www\engine\kernel\hook.php:94 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 30. call_user_func_array:{G:\server\www\engine\kernel\hook.php:79}() G:\server\www\engine\kernel\hook.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 31. fn_block_replace_e() G:\server\www\engine\kernel\hook.php:79 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 32. Block::replace() G:\server\www\lot\extend\plugin\lot\worker\block-e\index.php:9 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 33. preg_replace_callback() G:\server\www\lot\extend\block\engine\kernel\block.php:73 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 34. Block::{closure:G:\server\www\lot\extend\block\engine\kernel\block.php:69-73}() G:\server\www\lot\extend\block\engine\kernel\block.php:73 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 35. call_user_func_array:{G:\server\www\lot\extend\block\engine\kernel\block.php:72}() G:\server\www\lot\extend\block\engine\kernel\block.php:72 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 36. {closure:G:\server\www\lot\extend\plugin\lot\worker\block-e\index.php:4-9}() G:\server\www\lot\extend\block\engine\kernel\block.php:72 

[28-May-2017 09:17:01 Asia/Jakarta] PHP 37. ob_start() G:\server\www\lot\extend\plugin\lot\worker\block-e\index.php:5 

謝謝。

+0

我會盡量玩這個:http://php.net/manual/en/function.ob-get-level.php –

回答

0

您需要的ob_get_clean值賦值給一個變量

<?php ob_start(); ?> 
<html> 
    <head></head> 
    <body> 
    <?php ob_start(); ?> 
    <?php echo '$page->content'; ?> 
    <?php $inner = ob_get_clean(); ?> 
    </body> 
</html> 
<?php $outer = ob_get_clean(); ?> 
<?php var_dump($inner, $outer); ?> 

請注意,我把引號$頁面級>內容,以方便閱讀這個代碼,你應該不要使用這些引號。

+0

是的,我確實把輸出內容放在每次輸出變量緩衝區結束。這就是我使用輸出緩衝區的原因。但我不知道爲什麼有這個錯誤信息。 –