rreynier Posted May 21, 2009 Share Posted May 21, 2009 So, I was wondering if there is a way to capture things that are put outside the php opener and closer and save it to a variable. Basically.. say I have a function <?php function printMainNav() { ?> <ul id="mainNav"> <li><a href="#">Home</a></li> <li><a href="#">Customers</a></li> <li><a href="#">Tickets</a></li> <li><a href="#">Reports</a></li> </ul> <?php } ?> Now I call this function from somewhere else, but I dont actually want this to print to screen right then. I know I could just do something like <?php function printMainNav() { $html = '<ul id="mainNav">'; $html .= '<li><a href="#">Home</a></li>'; $html .= '<li><a href="#">Customers</a></li>'; $html .= '</ul>'; return $html; } ?> but this sux for a bunch of html and organzing it. Maybe there is a way to start reading what is being put out to screen and stop it, storing it, then call on this later when wanted. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/ Share on other sites More sharing options...
radi8 Posted May 21, 2009 Share Posted May 21, 2009 Since PHP is server side scripting (all processing on the server and then delivered to the client), what you are asking is not feasible with PHP without doing a page reload. BUT... the way I was able to do this was with div tags and javascript since javascript is client side and will control the layout without a page reload. Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839334 Share on other sites More sharing options...
rreynier Posted May 21, 2009 Author Share Posted May 21, 2009 so hmm.. alright. what would be the best way to do this: function main() { some logic include 'showPart.html.php'; $body = htmlForBody($x,$y); // htmlforbody function is found in showPart.html.php printPage($body); } function printPage($body) { printHeader(); echo $body; printFooter(); } since showPart.html.php is mostly all html ( i am trying to seperate logic and presentation ) is there any way to do this besides doing the $html = .'<div><ul></ul></div>' etcetc? Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839345 Share on other sites More sharing options...
cunoodle2 Posted May 21, 2009 Share Posted May 21, 2009 Could you just put the html code in a text file and then store the text contents in a variable? Then later output said variable to the screen when you want? Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839347 Share on other sites More sharing options...
jackpf Posted May 21, 2009 Share Posted May 21, 2009 What?? Why not just do this? <?php function printMainNav() { return '<ul id="mainNav"> <li><a href="#">Home</a></li> <li><a href="#">Customers</a></li> </ul>'; } $contents = printMainNav(); //do whatever echo $contents; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839365 Share on other sites More sharing options...
DarkSuperHero Posted May 21, 2009 Share Posted May 21, 2009 you could place all html inside a txt file like cunoodle2 said...and use file_get_contents; or file(); :-) Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839376 Share on other sites More sharing options...
rreynier Posted May 21, 2009 Author Share Posted May 21, 2009 ok so i think im really not explaining myself well.. lets say i wanted to do some logic to get the main content html i could start with printHeader(); and printNav(); then do this logic, but what if, for example you want to add a specific javascript or stylesheet to the header based on whatever logic happened.. problem is you cant, because you have already printed it to the browser. my current solution is to start a $html that just gets added to, instead of printed to screen while the logic happens. if the logic dictates that maybe i need to add another css file.. i can then add it to a value that could then get passed to printHeader($stylesheets). then at the end of all processing print the header and the nav and then echo the $html. Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839378 Share on other sites More sharing options...
rreynier Posted May 21, 2009 Author Share Posted May 21, 2009 Quote you could place all html inside a txt file like cunoodle2 said...and use file_get_contents; or file(); :-) i would like to avoid this.. because i could just add to a $html variable until the end and then output it with the rest of the final functions. but this creates messy unreadable code in my experience. this may sound stupid but editing $html = '<div id=mainContent>'; $html = '<h1>Title</h1>'; gets realllly messy and hard to read especially when you start having 100+ lines of html. Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839379 Share on other sites More sharing options...
rreynier Posted May 22, 2009 Author Share Posted May 22, 2009 I found the solution! output buffering. basically do ob_start(); then do some output.. $html = ob_get_contents(); then do ob_end_clean any performance issues with this ? Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839898 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 It depends. If you have pretty normal sized html source, it can actually speed up rendering because the browser receives all the html at once, rather than having to piece it together. However, if your source is huge, it'll just end up taking the browser longer. But, you can use stuff like compression with OB. So idk. I personally quite like it. Quote Link to comment https://forums.phpfreaks.com/topic/159153-store-stuff-to-a-variable-beforing-being-output-to-browser/#findComment-839929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.