0

For example…

This PHP code

<?php
echo '<html>';
echo '<body>';
echo '<h1>Header One</h1>';
echo '<p>Hello World!</p>';
echo '</body>';
echo '</html>';
?>

Generates this HTML markup

<html><body><h1>Header One</h1><p>Hello World!</p></body></html>

Are there any functions, libraries or configuration options to make PHP automatically apply some simple formatting (line breaks & indentation) based on the nesting of html tags generated in the output? So that instead something like this would be generated…

<html>
      <body>
            <h1>Header One</h1>
            <p>Hello World!</p>
      </body>
</html>
Corey
  • 816
  • 9
  • 19
  • 3
    [PHP Tidy](http://php.net/manual/en/book.tidy.php) does this. – Michael Berkowski Apr 03 '12 at 21:01
  • 2
    Why are you echo'ing the HTML markup? You can just type it outside of PHP tags – Ayush Apr 03 '12 at 21:01
  • Or, use a HEREDOC and format it yourself instead of echoing each line... – Michael Berkowski Apr 03 '12 at 21:02
  • The code I am working on generates a lot of html inside loops, and is much more complicated than the example code. I just wanted something simple to explain my question. – Corey Apr 03 '12 at 21:10
  • possible duplicate of [How to properly indent PHP/HTML mixed code?](http://stackoverflow.com/questions/1155799/how-to-properly-indent-php-html-mixed-code) – Gajus Feb 22 '14 at 12:43

7 Answers7

3

You could try templating engines like Smarty, Savant, PHP Sugar or VLib.

Or you could go commando with output handling (which I think is a hack). ob_start('ob_tidyhandler');

For the output handling, Tidy might not be installed or enabled, typically the package you will need to install is named php-tidy and you will need to uncomment extension=tidy in your php.ini

Ant0ine64
  • 177
  • 1
  • 12
mukama
  • 969
  • 2
  • 12
  • 28
1

You can put html in your PHP script without having to echo it. You also might want to look for a PHP template engine like smarty, so you can separate the view from logic.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
1

I prefer to use heredoc strings and format/indent the HTML myself. Mixing HTML strings inside PHP code quickly leads to unreadable, unmaintainable code. Some advantages of this method:

  • Quotes don't need to be escaped.
  • You can put variables inside the heredoc strings.
  • Even when working with code that loops, you can output the HTML inside heredoc strings. If these strings are indented properly relative to your other blocks of HTML, you will still get HTML code that has good indentation.
  • It forces you to think about when you want to print HTML, and to print it in blocks instead of lots of little pieces sprinkled throughout your code (very hard to read and maintain).

It's better to separate the PHP code from the HTML as much as you can, whether this means using a templating engine or just putting all of the code before all of the HTML. However, there are still times when it's easiest to mix PHP and HTML.

Here's an example:

<?php
$text = 'Hello World!';
echo <<<HTML
<html>
    <body>
        <h1>Header One, with some '"quotes"'</h1>
        <p>$text</p>
    </body>
</html>

HTML;
?>
We Are All Monica
  • 13,000
  • 8
  • 46
  • 72
  • I'm just curious if there is a way to have PHP do the work for me. I understand I can manually add the formatting and break in and out of PHP. – Corey Apr 03 '12 at 21:14
  • 2
    Corey: I'd look into `ob_tidyhandler` then, but this feels like a hack to me. Doing the formatting yourself leads to much cleaner code, which I think is very important for readability and maintainability. Getting and learning a good editor that understands indentation makes this easier. – We Are All Monica Apr 03 '12 at 21:17
  • "Mixing HTML strings inside PHP code quickly leads to unreadable, unmaintainable code." and yet `Heredoc` is esoteric and often _less_ readable than simply `?>` escaping back to HTML. It _might_ be beneficial if you need to embed a lot of `$variables` and don't want to use `= $shortTags ?>`, but it doesn't seem to be something that should be used without good reason, when better ways are available. – underscore_d Aug 15 '15 at 20:37
1

If I understand your question right, you want to pretty-print the HTML output.

This can be done by post-processing the output of your PHP script. Either by using PHP's output handling feature combined with the tidy extension­Docs:

ob_start('ob_tidyhandler');

Tidy is an extension specialized on cleaning up HTML code, changing indentation etc.. But it's not the only way.

Another alternative is to delegate the post-processing task to the webserver, e.g. output filters in Apache HTTP Server­Docs.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • could you expand on Tidy? is that line of code the only thing needed, or is there more to it? – Corey Apr 03 '12 at 22:18
  • That single line of code is initiatializing the output handling callback. See [`ob_tidyhandler`](http://php.net/ob_tidyhandler) and [`ob_start`](http://php.net/ob_start). I'm a bit unsure how to configure the tidy handler options from top of my head. You can also create your own handler, buffer and process the buffer with the tidy functions. – hakre Apr 03 '12 at 22:48
0

...Or you can use the "<<<" operator where you can set formation by yourself:

<?php

echo <<<DATA
<html>
      <body>
            <h1>Header One</h1>
            <p>Hello World!</p>
      </body>
</html>
DATA;
  • Nothing wrong with this answer. In fact I use this technique in my own code because it forces you to think about when you want to print HTML, and to print it in blocks instead of lots of little pieces sprinkled throughout your code (very hard to read and maintain). – We Are All Monica Apr 03 '12 at 21:06
0

You could do this (my preference):

<html>
      <body>
            <h1>Header One</h1>
            <p>Hello World!</p>
            <?php echo '<p>Hello Hello!</p>'; ?>
      </body>
</html>

Or:

<?php

$html = '<html><body><h1>Header One</h1><p>Hello World!</p></body></html>';

$tidy = new tidy();
$tidy->parseString($html, array('indent'=> true,'output-xhtml'=> true), 'utf8');
$tidy->cleanRepair();

echo $tidy;

?>";

...would print:

<html>
      <body>
            <h1>Header One</h1>
            <p>Hello World!</p>
      </body>
</html>
iambriansreed
  • 21,935
  • 6
  • 63
  • 79
  • I'm just curious if there is a way to have PHP do the work for me. I understand I can manually add the formatting and break in and out of PHP. – Corey Apr 03 '12 at 21:13
  • @Corey The last example uses `tidy` which will format the xhtml for you as original request. Read more: http://www.php.net/manual/en/tidy.examples.basic.php – iambriansreed Apr 03 '12 at 21:20
0

If the below code looks useful to generate html with php, try this library https://github.com/raftalks/Form

Html::make('div', function($html))
{
    $html->table(function($table)
    {
        $table->thead(function($table)
        {
            $table->tr(function($tr)
            {
                $tr->th('item');
                $tr->th('category');
            });
        });


        $table->tr(function($tr)
        {   
            $tr->td()->ng_bind('item.name','ng-bind');
            $tr->td()->ng_bind('item.category','ng-bind');

            $tr->setNgRepeat('item in list','ng-repeat'); //using second parameter to force the attribute name.
        });

        $table->setClass('table');
    });

   $html->setClass('tableContainer');
});
Raftalks
  • 2,017
  • 1
  • 21
  • 25