Rails 输出的 HTML 缩进会发生什么变化?

4

我正在使用TextMate为Rails视图编写代码(使用2空格缩进标准)。每当查看我的网页输出(查看源代码),HTML括号总是出现奇怪的缩进。例如,我的application.html.erb看起来像这样:

<!DOCTYPE html>
<html>
<head>
  <title>Rainleader</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :all %>
  <%= csrf_meta_tag %>
</head>
<body>
    <div id="outer">
        <div class="contentwidth main">
            <%= render 'layouts/header' %>
        </div>

        <%= yield %>

</body>
</html>

这里是渲染的部分内容(_header.html.erb):

<div class="logo">
    <h1><a href="index.html">minimal.</a></h1>
</div><!-- end logo -->

但是,输出的HTML代码中有错位(缩进不正确)的括号(请参见下面代码中的注释):

<body>
    <div id="outer">

        <div class="contentwidth main">
                        <div class="logo"> <<<Why is this so far to the right?
    <h1><a href="index.html">minimal.</a></h1>   <<<Why is this so far to the left?
</div><!-- end logo -->

这是怎么回事?如果我在application.html.erb中调用_header.html.erb partial并且缩进了四个空格,我需要至少缩进同样数量的代码才能正确嵌套吗?


你为什么关心输出HTML的缩进? - Mat
1
好问题,我只是喜欢有干净的代码。叫我怪异,但这让我不舒服。 - Trent Scott
当渲染在<pre>标签中时,缩进对我很重要。 - jwadsack
为了我的目的,我能够使用white-space: pre-line;来解决<pre>中的缩进问题。这对HTML没有帮助(但我不关心,因为我们会压缩所有内容)。 - jwadsack
1个回答

3
在application.html.erb中,被渲染的部分的第一行缩进为<%= render 'layouts/header' %>。但是所有其他代码行都不再缩进,就像在您的部分中一样左对齐。这也让我感到困扰,这也是我开始使用haml的原因之一。

有趣,不知道这个。谢谢 :) - Trent Scott
因此,这意味着行<%= render 'layouts/header' %>不应缩进。 - Zack Xu
我在haml中发现了一个类似的问题,除了第一行外,它缩进了所有行。 - jwadsack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接