douzhangkui2467
2012-02-02 06:53
浏览 57
已采纳

php ob_start()修复以前输出的标签

I just recently started toying with output buffering and seem to have run into a roadblock. My website utilizes a template system that loads html header/footer and the correct content template. Problem is the html headers are set in the first template loaded. So when (if) another template is loaded that contains headers (stored in a string $headers) it won't add them to the header. I have created a real sloppy way of doing this, I am looking for suggestions on to better handle this.

index.php

    ob_start('ob_html_headers');

the callback function

    function ob_html_headers($buffer)
    {
        global $headers;

        return str_replace('</head>', $headers.'</head>', $buffer);
    }

Any help is appreciated.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dppx9253 2012-02-02 07:02
    已采纳

    Hmmm, interesting question. If you are trying to do what I think you are, you'd probably be better off building up the data to be sent per-section, then echoing is all out the end. You can nest ob_start if you don't want to go about converting all your echos to $str .= quite yet.

    Basically what you seem to want to do is to allow later information to affect earlier output, the best way to do that is build a structure (don't worry a few arrays of strings could suffice) that represents your page, then "render" it at the end when you know where everything needs to go.

    打赏 评论
  • douguomou5094 2012-02-02 07:04

    I don't understand why "headers" being stored in the template.

    As far as I understand templating, whatever dynamic value used in the template, have to be defined in the business logic part (and most likely taken from the database).

    This way you will experience no problem like this.

    打赏 评论

相关推荐 更多相似问题