Improving readability in "View Source"

Hi,

I have a "formatting’ question. Basically, my php code below works great. But the problem is that when I view it as “source”, all of the lines are bunched together without any space or indenting.


						        <ul>
            <?php foreach ($sections as $section) {
				              echo '<li><a href="/Chapters/' . $section['chapter_name'] . '/' . $section['section_name'] . '.php">' . $section['topic'] . '</a></li>';}?>
									</ul>

Is there anyway that I can make it look better formatted (again, just in the “View Source”) so that my code looks cleaner as in something like what you see below?

Thanks so much!

-Eric

<ul>
					<li><a href="/Chapters/Linear_Functions/linear_relationships_making_connections.php">Making Connections</a></li>
					<li><a href="/Chapters/Linear_Functions/linear_introductory_problem.php">Introduction</a></li>
					<li><a href="/Chapters/Linear_Functions/linear_tables.php">Tables</a></li>
					<li><a href="/Chapters/Linear_Functions/linear_Equations.php">Equations</a></li>
					<li><a href="/Chapters/Linear_Functions/linear_Graphs.php">Graphs</a></li>
					<li><a href="/Chapters/Linear_Functions/linear_definition_slope1.php">Slope</a></li>
					<li><a href="/Chapters/Linear_Functions/linear_definition_yint1.php">The y intercept</a></li>
					<li><a href="/Chapters/Linear_Functions/linear_function_definition.php">The Linear Function</a></li>

You can add in things like newline characters to force a new line, and even tabs, too.

E.g. adding
at the end of the line will cause a line return:

echo '<li><a href="/Chapters/' . $section['chapter_name'] . '/' .
$section['section_name'] . '.php">' . $section['topic'] . '</a></li>[COLOR="Red"]\
[/COLOR]'

This is a complete waste of time and bloat for nothing. As long as valid HTML is being output the formatting doesn’t matter.

Why does it matter? no one expects the source to be pretty and those who do are just creating worthless sites anyway ie. totally static.

One little line return is bloat?

Its unnecessary so yes. Its not just one its several everywhere and if your using hierarchical MVC it will quickly get out of hand. If your going to do things that way you might as well create a true DOM object and output at the end with formatting turned on, which is pretty worthless but still more concrete then trying to indent and space what will probably be totally separate pieces of code.

This used to be more of an issue when you’d debug your front end code by viewing source. Now it’s far more comment to inspect the DOM using something like Firebug or the developer tools in Chrome Canary, so less necessary.

If you do want to format the output the
and \ characters will need to be inside double quotes to get parsed though.

Or do this, injects an OS independent line end;


echo "<option>". $row['yogurt'] ."</option>". PHP_EOL

That’s about the only time I use them, unless I am doing complex tables for tabular data of course - rare enough.

Thanks for the input, guys! Basically, I got a comment that html my looked very unreadable which is why I wanted to look into this. I was using PHP to generate a menu that will be in dynamic in nature (which I neglected to tell the commenting person); based on what I’m hearing here, perhaps it’s not worth it.

The person who said that is a moron.

In fact if you want to be a smart @ss you should place everything on a single line and explain that it is actually the better way to go reducing the page weight, increasing load time and improving SEO. That is if you want to be a smart @ss.

or just follow up with why it matters? Has this person ever heard of firebug or perhaps chrome development tools… I gather not in which case you probably shouldn’t listen to anything they say in the first place.

Totally agree and would love to know their reasoning .#

Thanks again. My original question was actually a CSS question. Being somewhat of a CSS newbie, the responder put a lot of time and energy into one of my posts, which led to discussing the presentation of my page, noncompliant html, and inefficiencies (there were other areas of “fat” that I may or may not deal with) and I think that they were really interested in getting me to think about clean code efficient code. However, the php crowd has won me over on this point!

Although this is pretty useless. A better way would perhaps be:


ob_start('outputhandler');

function outputhandler($buffer) {
	$doc = new DomDocument;
	$doc->loadHTML($buffer);
	$doc->formatOutput = true;
	return $doc->saveHTML();
}

//Generate code, output it in any old messy way.
//...


//At the end of the script
ob_end_flush();

But it’s entirely pointless, imho.

Well different contexts. If you’re showing your markup to someone to have them understand it or help improve it, then yes the formatting is important. Usually though the markup is not the content, it’s just browser fodder, so the formatting is not important.

Thanks you!

This is site is produced by me and for me so I think that I’m not going to worry about it.

-Eric