kc9ddi Posted December 2, 2006 Share Posted December 2, 2006 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 More sharing options...
keeB Posted December 2, 2006 Share Posted December 2, 2006 I don't want to say it's not, but this is definitely not the route you want to take, I can assure you.Let me start by asking a simple question, what do you want open_and_parse to actually [b]do[/b] Link to comment https://forums.phpfreaks.com/topic/29236-opening-a-file-and-php-parsing/#findComment-134024 Share on other sites More sharing options...
kc9ddi Posted December 2, 2006 Author Share Posted December 2, 2006 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 https://forums.phpfreaks.com/topic/29236-opening-a-file-and-php-parsing/#findComment-134028 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.