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>
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>
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
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.
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:
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;
?>
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
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
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 extensionDocs:
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 ServerDocs.
...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;
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');
});