2

I am wondering if there is any technique out there for making HTML code look good when printing that HTML (multiple lines) from a PHP function?

In particular, new lines and tabs. Obviously, I want the HTML tags behind-the-scenes to look good with the proper new lines and tabs. However, depending on when I call the PHP function that is printing the HTML, I might need to start the tabs at 1, 2, 3, 4, etc. I don't want to have to pass the starting tab count to the PHP function that is printing the HTML.

So, does anyone know a general-purpose way to print HTML-friendly code with PHP? Or is there a design principle that I am missing that says, "Don't print multiple HTML lines from a PHP function." ...

Thanks,

Steve

5 Answers 5

2

I wouldn't bother. With DOM-inspection tools like Firebug readily available, there's no point wasting time on making something designed for a computer to read look good. See this question for other people's opinions on the matter too.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you forgot a link on "this question". Thanks.
+1 for Firebug, every valid HTML/XML can be parsed, formatting is wasting time
1

Markup Cleanup

There are some wonderful codetools out there that will cleanup (ie, adjust spacing) and repair (ie, close open tags).

Tidy is one of the more popular html cleanup/repair tools. You can parse your script's output through Tidy on-the-fly using php's output buffers.

Zend Dev Zone Intro - http://devzone.zend.com/article/761 Tidy Docs in PHP Manual - http://www.php.net/manual/en/book.tidy.php

And now, a trivial example, yay!

<?php
ob_start();

echo '<div><b> messy</b> <div> <i>blarg</I></div>';
echo '  <div>code rawr!</div>';

$output = ob_get_clean();

$tidy = tidy_parse_string($output);
tidy_clean_repair($tidy);
echo $tidy;
?>

Templating System

You should be using a templating system to separate the logic (php code) and presentation (html/markup/etc) parts of your application.

Using this practice has many advantages including, among other things, making your code easier to maintain and debug.

This is a huge topic of discussion, and almost always goes into frameworks and MVC. There are thousands of resources, here are a few :P

Writing a Template System in PHP - http://www.codewalkers.com/c/a/Display-Tutorials/Writing-a-Template-System-in-PHP/
Separation of Business Logic from Presentation Logic in Web Applications - http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21

Comments

1

if you're generating valid xhtml, you can buffer your output and at the end, do that:

$dom = new DomDocument (('1.0', 'utf-8');
$dom->loadHtml (ob_get_clean ());
$dom->formatOutput = true; 
echo $dom->saveXML ();

if you're using a framework you can probably make this work as a plugin or filter. this way it's also work if you embed template into other template or use output from helper

Comments

0

Generating your HTML using a template system, and keeping your templates in order is a simple way of doing this

Comments

0

You shouldn't be printing html with php. Use php's shorthand syntax inside of a template.

<h1><?= $page_title ?></h1>
<p><?= page_content ?></p>

instead of

<?php

echo "<h1>" . $page_title . "</h1>";
echo "<p>" . $page_content . "</p>";

?>

5 Comments

Wow that makes so little sense. "You shouldn't be printing HTML with PHP, print it with PHP in another PHP file."
@nickf - if you look carefully youll see im not printing html at all.
what do you think $page_content and $page_title are?
@nickf - "HTML is written in the form of HTML elements consisting of tags, enclosed in angle brackets (like <html>)" $page_content is marked up by html, it is not itself html.
The question is clearly about how to neatly format HTML code with PHP. I don't think it's much of a stretch of the imagination to see that the HTML would be generated in PHP and stored into a variable named something like $page_content. eg: $page_content = "Hello <em>World</em>."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.