Forum Moderators: coopster

Message Too Old, No Replies

Include as Function

Here's how!

         

killroy

8:53 am on Apr 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Was just reading the snippets in the old PHP toolbox threads, and appearantly I'm not the only one who wants to execute a block of PHP code but hold on to the output for later use.

I used to program in HTAG before, and there including a file was like executing a function. It simply returned the text output of that file. In PHP the include and require functions/operators only return status codes.

I needed a different functionality for a simple skinning system. Basically I had a "skin" like this:

SKIN.PHP:

<html>
<body>
<h1><?=$pageTitle?></h1>
<div id="content"><?=$pageContent?></div>
<div id="footer"><?=$pageFooter?></div>
</body>
</html>

Then i'd have a php file for each type of page. In my index.php I'd first include PAGE.PHP and then SKIN.PHP.

This of course mean PAGE.PHP looked something like this:

PAGE.PHP
<?
$pageTitle="Title of Page";
$pageContent=<<<ENDOFCONTENT
Lots of Content here.
ENDOFCONTENT;
$pageFooter="<hr>Footer of Page";
?>

Of course if the content part, which could be really long might include a lot of in and out of PHP it could get messy. also, I couldn't just place include($contentFileName) inside SKIN.PHP since the title and footer needed to be set before execution got there.

So what I really wanted was a page file like this:

BETTERPAGE.PHP
<?
$pageTitle="Title of Page";
$pageFooter="<hr>Footer of Page";
?>Normal in and out of PHP in the <?echo $body;?>
of the page
<?include("otherfile.php");
?>

and then have the output placed in $pageContent.

This function accomplished this:

function runPHP($phpFileName){
ob_start();
include($phpFileName);
$runPHPResult=ob_get_contents();
ob_end_clean();
return $runPHPResult;
}

so in my INDEX.PHP I could have something like:

<?
$pageContent=runPHP('betterpage.php');
require('skin.php');
?>

Hopw anybody else might find this usefull!

regards,
SN

henry0

12:03 pm on Apr 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very nice SN,
Could you explain:
since the title and footer needed to be set before execution got there.

I use something different for the same result
The user choose its page look, bg etc...
Then I pass the values in for ex {BG_1}
Which goes to a template page that in return is governed by a class
However I output in one shot everything from header, content and footer with many “in and out” of PHP
So I guess I misunderstood you or did misread your post
Regards
Henry

killroy

12:13 pm on Apr 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I mean is, you couldn't write:

<body>
<h1><?=$pageTitle?></h1>
<div id="content"><?include $pageContentFileName?></div>
</body>

Since $pageTitle gets evaluated before the page file gets included. So if the included file defines $pageTitle it still wouldn't end up in the final place.

SN

henry0

12:32 pm on Apr 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks
Got it :)
Thank you for sharing
Henry

killroy

1:09 pm on Apr 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To Clarify, this is a quick and dirty CMS I used in a few sites. It consts of 3 parts. One or several skins, each defining a layout and a few plugin spots for content, title, menus and so on. Several page files that contain ONLY the neccessary code and content for each page (Here I like to use a little formatting script to avoid having to actually write any HTML or PHP even). And lastly, a main script that knows which page and/or skin to load.

SN