Jump to content

closing php tag in function to write html good practice?


johnmerlino1

Recommended Posts

I'm reading over this book and I came across something that looked very odd:


public function DisplayHeader()
{
    ?>
    <table width="100%" cellpadding="12"
    cellspacing="0" border="0">
    <tr bgcolor ="black">
    <td align ="left"><img src = "logo.gif" /></td>
    <td>
    <h1>TLA Consulting Pty Ltd</h1>
    </td>
    <td align ="right"><img src = "logo.gif" /></td>
    </tr>
    </table>
    <?php
}

In the function it closes a php tag, uses raw html that is rendered as is on the page, and then returns back to opening the php tag. I understand why it is being done. It is easier to write raw html here than to echo it in a php block, but it just looks odd. Anyone actually use this technique?

 

Anyone actually use this technique?

I wouldn't. I much prefer to go the template route. A *very* simple example:

 

<?php

function renderTemplate($template, array $data = [])
{
  extract($data);
  include $template;
}
template.html.php

<table width="100%" cellpadding="12" cellspacing="0" border="0">
  <tr bgcolor ="black">
    <td align ="left"><img src = "logo.gif" /></td>
    <td>
      <h1><?= $heading; ?></h1>
    </td>
    <td align ="right"><img src = "logo.gif" /></td>
  </tr>
</table>
<?php

renderTemplate('template.html.php', ['heading' => 'TLA Consulting Pty Ltd']);

I wouldn't. I much prefer to go the template route. A *very* simple example:

 

<?php

function renderTemplate($template, array $data = [])
{
  extract($data);
  include $template;
}
template.html.php

<table width="100%" cellpadding="12" cellspacing="0" border="0">
  <tr bgcolor ="black">
    <td align ="left"><img src = "logo.gif" /></td>
    <td>
      <h1><?= $heading; ?></h1>
    </td>
    <td align ="right"><img src = "logo.gif" /></td>
  </tr>
</table>
<?php

renderTemplate('template.html.php', ['heading' => 'TLA Consulting Pty Ltd']);

Yeah that looks much more eloquent. Obviously, the example in the book was for a trivial situation and something like MVC wasn't necessary, but still it looked bad.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.