8

As opposed to using an include, which executes the included php in the file...is it possible to save the contents of a php file to a variable - but with the php still intact and executable?

My goal looks something like:

$template = some_imaginary_include_function('myfile.php');
foreach($list_of_blogs as $blog) {
    // somehow get blog content in template and render template;
}

I know thats a dumb example...but I hope it illustrates the general idea. If I have to loop through the template 50 times on a page (say it is a list of blogs), it seems dumb to actually run and include for each.

Am I wrong? Is there a way to do this?

2
  • Sounds like premature optimisation. Use an opcode cache if you are worried about reading and compiling the file multiple times. Commented Jan 13, 2010 at 23:48
  • @BenJames Doesn't PHP natively optimize repeated inclusions? IIRC someone mentioned this in a discussion of the same matter; will source if found. Commented Oct 28, 2012 at 1:05

6 Answers 6

20

How about this...

function getTemplate($file) {

    ob_start(); // start output buffer

    include $file;
    $template = ob_get_contents(); // get contents of buffer
    ob_end_clean();
    return $template;

}

Basically, this will get whatever $file is, and parse it with PHP, then return the output into a variable.

Sign up to request clarification or add additional context in comments.

5 Comments

so thats cool...but will $template preserve the PHP stuff in the file? or will it turn into plain text?
As OcuS said, it will be parsed and executed: e.g. <?php echo PATH_BASE;?> will become /yoursite/ etc
k, alex...last question...for each iteration of the loop, do i just assign the vars required by the template, and then "echo $template"? thanks for your time
There is no point to this, as to get different output from the template you would need to call getTemplate more than once, hence include $file; would run more than once.
Well you could call getTemplate() and then do str_replace('{VAR}', $var, $template) if you wanted.
4

By using $content = file_get_contents('/path/to/your/file.php'); all the PHP tags will be preserved, you can then eval() or tokenize them to do whatever you want.

2 Comments

+1 Despite the security and performance implications that nay-sayers can pull from their hats, I believe this is the correct answer as per the question.
eval ... cough .. evil... sorry about that.
1

Doing the include into the loop is not SO dumb.

All variables defined before the include will be accessible into your template.

Keep it simple !

== EDIT ==

Or maybe you could improve alex's answer :

function getTemplate($file, $template_params = array()) {

    ob_start(); // start output buffer
    extract($template_params); // see PHPDoc
    // from here $var1 will be accessible with value "value1"
    // so your template may contain references to $var1

    include $file;
    $template = ob_get_contents(); // get contents of buffer
    ob_end_clean();
    return $template;

}
echo getTemplate('your_template.php', array('var1' => 'value1'));

(Not so simple anymore ^^)

4 Comments

haha, K.I.S.S. - keep it simple stupid. best advice i ever got. hurts my feelings every time. good call, thanks:)
question though...i literally expect for some pages to have 50-100 listings to iterate through...do you think it will start effecting performance if i include every time?
I'm sure it's fast and I guess php-engine has a caching system so the template is not completly parsed every time (not sure 'bout that, big big guess :))
You're still including the file multiple times. But now you've added output control functions and other stuff... so it's like to perform worse.
0

Write a function in the included PHP script that returns the desired output. Define a constant in the main PHP script. In the included PHP script, check for absence of said constant and echo the return value of the function if this is the case.

Comments

0

Even though it is often purported to be evil, you could try using eval() along with get_file_contents()

1 Comment

I use eval() in my website's basic CMS. Woops.
0

If you're developing in 5.3 it's much easier but even on 5.2 you can use what's called an anonymous function to do this.

An anonymous function will allow you to pass code around as a variable. To load this code from a file you may have to file_get_bytes into a string, eval that, then put in in a variable but you get the point I hope.

5.3: Anonymous functions

5.2: create_function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.