views:

216

answers:

5

I know it's really a subjective question, but for best-practices (and readability), I can't seem to get a fix on the best way to format long strings of HTML. I typically do it like this:

echo '
<div>
    <p>Content Inside</p>
    <div class="subbox">
        <ul>
            <li>etc.</li>
            <li>etc.</li>
            <li>etc.</li>
            <li>etc.</li>
        </ul>
    </div>
</div>
';

But I still don't like the outcome, especially if this appears in the middle of a large block of code. It just feels messy.

+3  A: 

Why not just embed your PHP in the HTML? That's how PHP was originally designed to work.

// PHP goes here
?>
<div>
    <p>Content Inside</p>
    <div class="subbox">
        <ul>
            <li>etc.</li>
            <li>etc.</li>
            <li>etc.</li>
            <li>etc.</li>
        </ul>
    </div>
</div>
<?php
// More PHP here
John Conde
+6  A: 

You can jump out of PHP and input HTML directly:

<?php $var = "foo"; ?>
<div>
  <ul>
    <li>Foo</li>
  </ul>
</div>
<?php $foo = "var"; ?>

If all you're doing is an echo/print, I think this is much cleaner. Furthermore, you don't need to run through and escape single/double quotes within the HTML itself.

If you need to store the HTML in a variable, you could use HEREDOC:

$str = <<<EOD
<div>
  <ul>
    <li>Foo</li>
  </ul>
</div>
EOD;
Jonathan Sampson
Yeah, I do this too, but I'm referring specifically to those instances where I have to embed long strings of HTML into PHP (like for an AJAX function, for instance). I'm still new to a lot of this stuff, but it seems to me this methodology can only get you so far, and there are simply going to be times where you need to embed sizeable strings of HTML into PHP, unless I'm just doing it completely wrong...
dclowd9901
@dclowd9901: Your example of using `echo` doesn't require the string to be in PHP itself. With AJAX, you are just sending around plain text. So whether it starts as HTML, or a printed PHP String, it makes no difference. Hope this helps!
Jonathan Sampson
It absolutely does, thanks so much for the explanation!
dclowd9901
@dclowd9901: No problem, keep up the good work!
Jonathan Sampson
+1  A: 

I would do it like this:

echo '<div>
         <p>Content Inside</p>
         <div class="subbox">
             <ul>
                 <li>etc.</li>
                 <li>etc.</li>
                 <li>etc.</li>
                 <li>etc.</li>
            </ul>
         </div>
      </div>';
murze
+2  A: 

Old school Perl-style heredoc works too:

echo <<<EOL
<div>
    <p>Content Inside</p>
    <div class="subbox">
        <ul>
            <li>etc.</li>
            <li>etc.</li>
            <li>etc.</li>
            <li>etc.</li>
        </ul>
    </div>
</div>
EOL;

Using multiple <?php ?> blocks is cleaner, but if you're in a class definition or something, a heredoc might be less awkward (though printing HTML from a class definition is nothing but awkward).

tadamson
A: 

The best way is to use a templating system where you can separate your logic from presentation. In the template you'd only have stuff like:

<html>
<body>

<h1><?php echo $title; ?></h1>
<p><?php echo $myparagraph; ?></p>

</body>
</html>
Lotus Notes