Jump to content

Opening a file and PHP Parsing


kc9ddi

Recommended Posts

Hi -

I'm trying to implement a simple templating system for my site.  I'm trying to do something resembling the MVC paradigm.  So, I have a model/controller php script, and then a view php script.  The model/controller does all the logic, and the view has mostly html with a few <?= $template['variable']; ?> to put in dynamic content.

I'm wondering if its possible to open the view php script, parse the php, but store the result into a variable, rather than outputing it directly to the browser.

For example:

view.php:
[code]
<p>Hello There</p>
<p>Here is a message: <?= $template['message']; ?></p>
[/code]

controller.php
[code]
<?
$template['message'] = "PHP Is cool.";
$myVariable = open_and_parse('view.php');
?>
[/code]

And then have $myVariable contain "<p>Hello There</p>\n<p>Here is a message: PHP is cool.</p>"

Is this possible?
Link to comment
https://forums.phpfreaks.com/topic/29236-opening-a-file-and-php-parsing/
Share on other sites

Open the file passed as an argument, parse the PHP within the file, and return a string containing its parsed contents.  Right now this is what I have (altered from an example in the PHP Manual:)

[code]
function get_include_contents($filename) {
extract($GLOBALS,EXTR_SKIP);
if(is_file($filename)) {
ob_start();
include($filename);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
[/code]

I do think its pretty ugly (particularly the extract($GLOBALS) part), but its the only thing I could come up with.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.