开发者

Run function before including contents of file?

开发者 https://www.devze.com 2023-03-21 04:10 出处:网络
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 d

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

精彩评论

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