title: HTML Indentation in the mVc World
body: <p>Here is a question that has been bugging me for a while, <em>nowadays</em> 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:</p>
  
  <p><strong>HTML Code:</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Testing MVC Indentation&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;?php View('h1', 'I am a Level 1 Header'); ?&gt;
  
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;?php View('h1', 'I am a Level 1 Header Inside a Table'); ?&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>To indent correctly, the first call to the <code>h1</code> view (or <em>partial</em>) should return:</p>
  
  <pre><code>\t&lt;h1&gt;I am a Level 1 Header&lt;/h1&gt;
  </code></pre>
  
  <p>While the second call to the <code>h1</code> view should return:</p>
  
  <pre><code>\t\t\t\t&lt;h1&gt;I am a Level 1 Header Inside a Table&lt;/h1&gt;
  </code></pre>
  
  <p>The <code>h1</code> view however, has no idea of the indentation scope it's in, so how the hell can it return the data properly indented? Also, <strong>ignoring indentation <em>in views</em> can disclose part of the application logic</strong> (<em>check the HTML source code of this page after <code>&lt;div id="content"&gt;</code> for a real-world example</em>):</p>
  
  <pre><code>&lt;body&gt;
  
  &lt;h1&gt;I am a Level 1 Header&lt;/h1&gt;
  
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;
  &lt;h1&gt;I am a Level 1 Header Inside a Table&lt;/h1&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>Not indenting at all solves all problems, but it also makes it harder to read and maintain:</p>
  
  <pre><code>&lt;body&gt;
  
  &lt;h1&gt;I am a Level 1 Header&lt;/h1&gt;
  
  &lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;h1&gt;I am a Level 1 Header Inside a Table&lt;/h1&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>The only feasible solution I see to this problem is by using <strong><a href="http://php.net/manual/en/book.tidy.php" rel="nofollow noreferrer">Tidy</a> and <a href="http://php.net/manual/en/book.outcontrol.php" rel="nofollow noreferrer">output buffering</a></strong>, but I wonder if it's worth the effort since it will make processing and loading <em>unnecessarily (?)</em> slow. Also, this will not make it easier the maintain the HTML code since it only indents the output, not the source.</p>
  
  <p>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.</p>
  
  <p>Any solutions / insights on this subject?</p>
  
  <hr>
  
  <p><strong>Related Questions:</strong></p>
  
  <ul>
  <li><strong><a href="https://stackoverflow.com/questions/2167524/do-you-indent-your-html-code">Do you indent your HTML code?</a></strong></li>
  <li><strong><a href="https://stackoverflow.com/questions/304055/do-you-generate-nicely-formatted-html">Do you generate nicely formatted HTML?</a></strong></li>
  <li><strong><a href="https://stackoverflow.com/questions/1155799/how-to-properly-indent-php-html-mixed-code">How to properly indent PHP/HTML mixed code?</a></strong></li>
  </ul>