1

I can write HTML code within a string and return it but it always looks a bit messy. Is there any better solution for this?

Example 1 - New line HTML

New line for every row with HTML. A very messy way to write HTML code.

function my_function()
{
    $html = '<div class="hello">';
    $html .= 'A very long text';
    $html .= '</div>';
    return $html;
}

Example 2 - HTML block

Still a messy way to write HTML code. Another downside is that it's harder to write 'cite' characters.

function my_function()
{
    $html = '
        <div class="hello">
            A very long text
        </div>';

    return $html;
}

Example 3 - ob_get_contents

I could use ob_get_contents. It separates my PHP from my HTML, nice! But I have read that it is bad performance.

function my_function()
{
    ob_start();
    ?>
        <div class="hello">
            A very long text
        </div>';
    <?php
    $html = ob_get_contents();
    ob_end_clean();
    return $html;
}

Question

Is there another way to keep HTML from PHP in a nice way?

Jens Törnell
  • 23,180
  • 45
  • 124
  • 206
  • You've probably thought of this already, but just in case - is there a reason you couldn't have the HTML in a template, and load that? – Mike Feb 15 '13 at 14:51
  • White spaces in the source code causes performance issues , there is a templating libraries . But I don't think you need such thing – Seder Feb 15 '13 at 14:55
  • @Grant Thomas Not in PHP in rendering the HTML file on the browser since the HTML file size will be bigger, you can check google page speed. – Seder Feb 15 '13 at 15:02

4 Answers4

1

You could use a form of templating, where your HTML structure is stored in template files with placeholder values, which you read in, and, replace the placeholder values with the real content.

That way you're just reading in a string, doing a bunch of 'replaces' to transform, and returning the result.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
1

Yes and no,

either of the 3 examples are far form the objective of "keep php away from html". And within tthem they arn't any better than the other.

It's not considered a good practice to keep the presentation layer connected with the logic.
Neither solution is what we could call straight forward for beginners. The simplest with the shorter learning curve would be to just use a template engine, like smarty.

http://www.smarty.net/

It would help you keep your presentation somewhat away from your code.

But the commonly accepted strategy for that is MVC separating the software within 3 layers. Model View and Controller. This article gives a simple explanation of what is mvc focused on php.

http://onlamp.com/pub/a/php/2005/09/15/mvc_intro.HTML

There are other architectures and practices that many consider better but this are the basics i believe.

Danilo Kobold
  • 2,562
  • 1
  • 21
  • 31
0

"Is there another way to keep HTML from PHP in a nice way?"

  • Yes - You should read about MVC and how to implement it in PHP (eg. using Smarty)
pwolaq
  • 6,343
  • 19
  • 45
0

Simple idea keeping the two apart:

//php function
    function stuff(){
          $arr = array();
          $arr['text'] =  'A very long text';
          $arr['class']  = 'hello';


        return $arr;
        }

HTML page:

<html>
<?php $thing = stuff(); ?>


<div class="<?php echo $thing['class']; ?>">
<?php echo $thing['text']; ?>
</div>

</html>
matpol
  • 3,042
  • 1
  • 15
  • 18