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: