johnmerlino1 Posted July 30, 2014 Share Posted July 30, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/290189-closing-php-tag-in-function-to-write-html-good-practice/ Share on other sites More sharing options...
Solution trq Posted July 30, 2014 Solution Share Posted July 30, 2014 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']); Quote Link to comment https://forums.phpfreaks.com/topic/290189-closing-php-tag-in-function-to-write-html-good-practice/#findComment-1486463 Share on other sites More sharing options...
johnmerlino1 Posted July 30, 2014 Author Share Posted July 30, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290189-closing-php-tag-in-function-to-write-html-good-practice/#findComment-1486467 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.