tags:

views:

229

answers:

6

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?

A: 

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.

Ignacio Vazquez-Abrams
+1  A: 

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 it will return the output as a variable.

alex
so thats cool...but will $template preserve the PHP stuff in the file? or will it turn into plain text?
johnnietheblack
it will be executed
OcuS
As OcuS said, it will be parsed and executed: e.g. <?php echo PATH_BASE;?> will become /yoursite/ etc
alex
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
johnnietheblack
That should work I believe.
alex
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.
Ben James
Well you could call `getTemplate()` and then do `str_replace('{VAR}', $var, $template)` if you wanted.
alex
A: 

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

zipcodeman
I use `eval()` in my website's basic CMS. Woops.
Nick Bedford
A: 

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

Chuck Vose
A: 

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 ^^)

OcuS
haha, K.I.S.S. - keep it simple stupid. best advice i ever got. hurts my feelings every time.good call, thanks:)
johnnietheblack
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?
johnnietheblack
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 :))
OcuS
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.
Ben James
+1  A: 

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.

Alix Axel