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
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.