Jump to content

closing php tag in function to write html good practice?


Go to solution Solved by trq,

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?

 

  • Solution

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.

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.