开发者

Run function before including contents of file?

Currently, I'm including my HTML "view" like so:

include $path_to_view;

I would like to be able to run a function on the contents of the view file before I include it, but I'm not sure how to do that and still include the view without using eval:

I basically want to do an "include"开发者_如何学Go type operation on the results of a function. Something like this:

include foo_function(file_get_contents($path_to_view));

How can I accomplish this, or is eval my only option?

Edit

I am using output buffers currently. I am not looking to change the view contents on disk, I just want to get the contents of the view, run a function on those contents, and "include" the contents so they are evaluated.


It sounds like you're looking for output buffering. Look at ob_start.

<?php

function callback($buffer)
{
  // Do something useful here.

  return $buffer;
}

ob_start('callback');

include $path_to_view;

ob_end_flush();


Look into output buffers. You can put the contents of a file into a var, then parse it as usual.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜