Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting of dynamically generated HTML - does no one care?

I have very little experience in web development, so this may be a very basic question.

It's just, from the limited experience I do have (a little PHP, and a little Ruby on Rails), it seems that the way dynamically generated HTML is formatted just "doesn't matter"; it ends up ugly, with weird indentation, and nobody cares because that isn't what the users sees.

Unless, of course, the user is a developer, or even just someone who's curious to look at a little HTML to try and learn something.

Maybe you don't know what I'm talking about; so let me give an example.

In a Ruby file I might have some code like this:

<h1>Heading</h1>

<div>
    <%= render :partial => '/layouts/body' %>
</div>

Then, in my "/layouts/_body.html.erb" file, I might have this:

<p>Here is some content!</p>

<ul>
    <li>List item 1</li>
    <li>List item 2</li>
    <li>List item 3</li>
</ul>

When all this gets rendered, it'll look fine. But if the user tries to view the source, the HTML will look pretty crappy:

    <h1>Heading</h1>

    <div>
        <p>Here is some content!</p>

<ul>
    <li>List item 1</li>
    <li>List item 2</li>
    <li>List item 3</li>
</ul>

    </div>

Obviously, this is not a big deal. And I can totally understand if the prevailing opinion is simply "It doesn't matter." But is that just the way it has to be? Does the readability of HTML not matter to anyone?

I'm just curious to know if this ever bugged anyone else enough for him/her to come up with a "solution" for it (obviously it would have to be someone who viewed it as a "problem" in the first place).

like image 794
Dan Tao Avatar asked Oct 13 '10 23:10

Dan Tao


2 Answers

Semantically correct validated HTML is important; very important. A few spaces and line breaks are not important - any decent formatter can resolve this easily. Most of the time nothing apart from a browser will view it, so it really isn't that important compared to being validated.

Of course if you do take the time to indent and format it will look better to those of us who bother to look..

like image 115
Richard Harrison Avatar answered Oct 12 '22 09:10

Richard Harrison


The HAML templating engine was specifically created with the goal of generating properly indented, nested and formatted HTML. There's no reason why other templating engines couldn't do the same.

So, yes, it is just laziness.

like image 22
Jörg W Mittag Avatar answered Oct 12 '22 11:10

Jörg W Mittag