Can I configure PHP to automatically format the HTML it generates?

Go To StackoverFlow.com

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>
2012-04-03 20:59
by Corey
PHP Tidy does this - Michael Berkowski 2012-04-03 21:01
Why are you echo'ing the HTML markup? You can just type it outside of PHP tag - xbonez 2012-04-03 21:01
Or, use a HEREDOC and format it yourself instead of echoing each line.. - Michael Berkowski 2012-04-03 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 2012-04-03 21:10
possible duplicate of How to properly indent PHP/HTML mixed code?Gajus 2014-02-22 12:43


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>
2012-04-03 21:04
by iambriansreed
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 2012-04-03 21:13
Yes. Will update - iambriansreed 2012-04-03 21:15
@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.ph - iambriansreed 2012-04-03 21:20


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');

2012-04-03 21:13
by mukama
+1 for actually answering the question, rather than going on a rant : - jnylen 2012-04-03 21:18
Nice nice. ob_tidyhandler is new one to me. Nice - iambriansreed 2012-04-03 21:22


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.

2012-04-03 21:02
by huysentruitw


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;
?>
2012-04-03 21:05
by jnylen
Downvoter: care to comment - jnylen 2012-04-03 21:06
Valid code +1 ; - Dave Thomas 2012-04-03 21:08
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 2012-04-03 21:14
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 - jnylen 2012-04-03 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 2015-08-15 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.

2012-04-03 21:25
by hakre
could you expand on Tidy? is that line of code the only thing needed, or is there more to it - Corey 2012-04-03 22:18
That single line of code is initiatializing the output handling callback. See <code>ob_tidyhandler</code> and <code>ob_start</code>. 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 2012-04-03 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;
2012-04-03 21:04
by Alexander Blacks
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) - jnylen 2012-04-03 21:06


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');
});
2013-01-15 16:47
by Raftalks
Ads