Page 1 of 1

Embeding HTML into PHP

Posted: Fri Mar 06, 2009 2:48 pm
by diG1tY
Hello. In a book by WROX press I read that it's not a good idea embedding HTML into PHP where it is not needed. It states basiclly that it's better to design your layout in HTML and parse things to PHP that are only needed. Is this true ? Does it make any difference if you write :

Code: Select all

 
<p>Some text and tags</p>
versus

Code: Select all

<?php echo "<p>Some text and tags</p>?>
Is it better to put only the things related to PHP or it doesn't matter how you end up embeding "on random'?

Re: Embeding HTML into PHP

Posted: Fri Mar 06, 2009 2:56 pm
by Benjamin
I'd say a good rule to go by is that you should not use PHP to print static data.

Re: Embeding HTML into PHP

Posted: Fri Mar 06, 2009 4:54 pm
by Benjamin
lkjkorn19 wrote:I guess it depends on the context as well. If you're going to generate a list, it would be ridiculous to load up data (in PHP), stop PHP, type that one "<h1>List</h1><ul>" line, get back into PHP and start listing things up.
If you're to do a longer paragraph that explains something (for example), exiting PHP is advised just principally for overview reasons. Like that, you don't have to worry about escaping quotes and whatnot. :)
Actually, in templating you are not supposed to incorporate logic, other than simple if, else, for and foreach loops. So your controller would generate the list using data it retrieves from the model. Then in your template it would look like this:

Code: Select all

 
<ul>
  <?php foreach ($listItems as $item): ?>
    <li><?php echo $item; ?></li>
  <?php endforeach; ?>
</ul>