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.
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?
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.
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.
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.
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();
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.