views:

7957

answers:

8

I want to conditionally output HTML to generate a page, so what's the easiest way to echo multiline snippets of HTML in PHP 4+? Would I need to use a template framework like SmartyPants?

    echo '<html>' + '\n'; // I'm sure there's a better way!

    echo '<head>' + '\n';
    echo '</head>' + '\n';

    echo '<body>' + '\n';
    echo '</body>' + '\n';

    echo '</html>' + '\n';
+13  A: 

You could use the alternative syntax alternative syntax for control structures and break out of php:

<?php if ($something): ?>
    <some /> <tags /> <etc />
    <?=$shortButControversialWayOfPrintingAVariable ?>
    <?php /* A comment not visible in the HTML but is a bit of a pain to write */ ?>
<?php else: ?>
    <!-- else -->
<?php endif; ?>
Tom Haigh
@Jeremy: This is probably the best, most direct way assuming you are not looking for something more... Are you looking for something more?
Frank V
Yes, specifically the ability to use PHP comments in between the HTML, comments that will not be echo'd.
Jenko
+6  A: 

try like this:

$variable = <<<XYZ
<html>
<body>

</body>
</html>
XYZ;
echo $variable;
lfx
php supports heredocs? nice.
seth
+3  A: 

I am partial to this style:

  <html>
    <head>
<%    if (X)
      {
%>      <title>Definitely X</title>
<%    }
      else
      {
%>      <title>Totally not X</title>
<%    }
%>  </head>
  </html>

I do use ASP-style tags, yes. The blending of PHP and HTML looks super-readable to my eyes. The trick is in getting the <% and %> markers just right.

John Kugelman
Are ASP tags compatible with PHP 4+?
Jenko
Yes but get em while they last - they're being removed from PHP (not sure which version... if they haven't gone already)
Greg
They should have been removed before they were added.
fiXedd
A: 

There's heredoc, where you do a thing like this:

<?php 
  $var = <<< BUFF
 <html>
   <lmnop />
 </html>
BUFF;
?>

The string following the <<< becomes the token that indicates when to stop buffering and resume parsing. As above, to invoke it, YOU MUST HAVE NO LEADING SPACES OR INDENTATIONS, must be just that word and a semicolon on that line.

PHP Manual has more on this.

cookiecaper
This has nothing to do with output buffering. It's heredoc syntax for strings.
Michael Madsen
Ahh, that's right. I've gotten my terms confused, apparently. Sorry. I'll fix it.
cookiecaper
That looks really confusing to read. If that's mixed throughout code it must be a nightmare to read and maintain.
DisgruntledGoat
+12  A: 

You can do a few things:

In Between PHP Tags

if(condition){

?>

<!-- HTML here -->

<?

}

In Echos

if(condition){

echo "HTML here";

}

With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:

echo '<input type="text">';

Or you can escape them like so:

echo "<input type=\"text\">";

As for template systems, Smarty is good for separating the design (Presentation Logic) from the coding (Business Logic). It makes the code a lot cleaner and easier to maintain.

If you have any more questions feel free to leave a comment.

Best of Luck!!

Chris B.
What an excellent solution for HTML within conditions.
Jenko
Oh, and thank you so much!
Jenko
No problem! Good Luck!!
Chris B.
Shouldn't the "/* HTML here */" REALLY be a "<!-- HTML here -->" ? ;)
fiXedd
Yes on the first post you are correct. I was caught up in the code haha. Thank you.
Chris B.
+2  A: 

Basically you can put HTML anywhere outside of PHP tags. It's also very beneficial to do all your necessary data processing before displaying any data, in order to separate logic and presentation.

The data display itself could be at the bottom of the same PHP file or you could include a separate PHP file consisting of mostly HTML.

I prefer this compact style:

<?php
    /* do your processing here */
?>

<html>
<head>
    <title><?=$title?></title>
</head>
<body>
    <?php foreach ( $something as $item ) : ?>
        <p><?=$item?></p>
    <?php endforeach; ?>
</body>
</html>

Note: you may need to use <?php echo $var; ?> instead of <?=$var?> depending on your PHP setup.

DisgruntledGoat
A: 

Another approach is put the HTML in a separate file and mark the area to change with a placeholder [[content]] in this case. (You can also use sprintf instead of the str_replace.)

$page = 'hello world';
$content = file_get_contents('html/welcome.html');
$pagecontent = str_replace('[[content]]',$content,$page);
echo($pagecontent);

Alternatively you can just output all the php stuff to the screen captured in a buffer, then write the html, then put the php output back into the page.

It might seem strange to write the php out, catch it, then write it again, but it does mean that you can do all kinds of formatting stuff (Heredocs etc),& test it outputs correctly without the hassle of the page template getting in the way. (Joomla CMS does it this way, BTW) ie:

<?php
ob_start();
echo('hello world');
$php_output = ob_get_contents();
ob_end_clean();
?>
<h1> My Template page says </h1>
<?php
echo($php_output );
?>
<hr>
template footer
julz
A: 

thjanks for share

youtube