Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Indentation in the mVc World [duplicate]

Here is a question that has been bugging me for a while, nowadays it's considered a good practice to indent HTML code but I've some reservations indenting code in a MVC pattern, here is a (silly) example:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<title>Testing MVC Indentation</title>
</head>

<body>

<?php View('h1', 'I am a Level 1 Header'); ?>

    <table>
        <tr>
            <td>
                <?php View('h1', 'I am a Level 1 Header Inside a Table'); ?>
            </td>
        </tr>
    </table>

</body>
</html>

To indent correctly, the first call to the h1 view (or partial) should return:

\t<h1>I am a Level 1 Header</h1>

While the second call to the h1 view should return:

\t\t\t\t<h1>I am a Level 1 Header Inside a Table</h1>

The h1 view however, has no idea of the indentation scope it's in, so how the hell can it return the data properly indented? Also, ignoring indentation in views can disclose part of the application logic (check the HTML source code of this page after <div id="content"> for a real-world example):

<body>

<h1>I am a Level 1 Header</h1>

    <table>
        <tr>
            <td>
<h1>I am a Level 1 Header Inside a Table</h1>
            </td>
        </tr>
    </table>

</body>

Not indenting at all solves all problems, but it also makes it harder to read and maintain:

<body>

<h1>I am a Level 1 Header</h1>

<table>
<tr>
<td>
<h1>I am a Level 1 Header Inside a Table</h1>
</td>
</tr>
</table>

</body>

The only feasible solution I see to this problem is by using Tidy and output buffering, but I wonder if it's worth the effort since it will make processing and loading unnecessarily (?) slow. Also, this will not make it easier the maintain the HTML code since it only indents the output, not the source.

I'm sorry for the "basic" question, but I've been focusing on business logic for the last years and I've been kinda disconnected with the presentation world - in the good old days my HTML code was all unindented, but then again I also used tables to design the layout - just trying to catch up now.

Any solutions / insights on this subject?


Related Questions:

  • Do you indent your HTML code?
  • Do you generate nicely formatted HTML?
  • How to properly indent PHP/HTML mixed code?
like image 265
Alix Axel Avatar asked Feb 20 '10 13:02

Alix Axel


3 Answers

I would say, that there is a difference between HTML and Source Code Indentation. In a typical MVC View you use source code which I think should be indented in a readable fashion.

The actual HTML output though is a different thing. Of course chaoticly indented source looks kind of unprofessional (this is why I sometimes tend to just remove any indentation at all but that usually involves as you said some HTML post processing), but for the Browser itself if doesn't matter at all and if I really need to view the source I will use a formatter or Firebug which shows all DOM elements in a nicely formatted way.

like image 100
Daff Avatar answered Oct 06 '22 19:10

Daff


As mentioned there is a difference between source code indentation and output indentation. The indentation of the source code can be different from the indentation of the output, because the output is constructed dynamically. When writing templates, the readability of the source code is most important. You should focus on that first! Usually the HTML indentation of the output is a non-issue.

Having said that, two practical approaches to get rid of indentation mismatch in your output:

Use a templating language that can properly indent the output

One example of the first is Haml, a templating language for Ruby. It takes a template like this:

%body
  %h1 I am a Level 1 Header
  %table
    %tr
      %td
        %h1 I am a Level 1 Header Inside a Table

The output will be neatly formatted HTML similar to your example, all properly indented.

One obvious disadvantage of this approach is that you're not really writing HTML in your templates, you're writing in a different language with HTML-like semantics. This can be a problem, depending on who will maintain the views.

Strip all superfluous whitespace from the output, don't indent at all

Some template languages have options to remove all superfluous whitespace. Smarty for PHP has a whitespace trimming plugin that does this job nicely, for example. It completely works around the output beautification problem by purposely making all output equally non-indented. It also saves a very marginal amount of bandwidth.

A PHP-only solution would be to use ob_start() with its $output_callback handler (which is documented here). You can write a simple callback that strips the excessive whitespace, similar to the Smarty plugin. Contrary to using Tidy in the output callback, this will still allow you to flush the buffer before the end of the page to speed up long/slow pages.

like image 39
molf Avatar answered Oct 06 '22 18:10

molf


Sorry for adding a third answer! It's a been a while, and I'd rather add an answer, please don't down-vote me for that. I just think this should be said here:

This seems to work, aka having 2 different "threads" of indentation, one for PHP and one for HTML:

<table>
<?foreach($list->sections as $sKey => $section) { ?>
    <tr><td><h4><?= $section->label ?></h4></td></tr>
<?  foreach($section->items as $iKey =>  $item) { ?>
    <tr><td><?= $item->label ?></td></tr>
<?  } ?>
<?} ?>
</table>

By always having the php tag start at the beginning of the line, and indent inside the php block code, the resulting code stays clean:

<table>
    <tr><td><h4>Books 1</h4></td></tr>
    <tr><td>Some Book</td></tr>
    <tr><td>Some Other Book</td></tr>
    <tr><td><h4>Books 2</h4></td></tr>
    <tr><td>Some Book in List 2</td></tr>
</table>

And the indentantion logic of the PHP and of the HTML are kept in the source file, albeit each on it's own.

like image 36
Rolf Avatar answered Oct 06 '22 19:10

Rolf