Jump to content

Store stuff to a variable beforing being output to browser.


rreynier

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.