输出的 HTML 中的 Rails 缩进会发生什么情况?
我正在 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 部分的调用缩进了四个空格,我是否需要将部分中的代码缩进至少相同的量才能使其正确嵌套?
I'm writing code for a Rails view in TextMate (using the 2-space indentation standard). Whenever I view the output of my webpages (View Source), the HTML brackets always seem weirdly indented. For example, my application.html.erb looks like this:
<!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>
And the partial it's rendering (_header.html.erb) looks like this:
<div class="logo">
<h1><a href="index.html">minimal.</a></h1>
</div><!-- end logo -->
But, an excerpt of the outputted HTML has misplaced (mis-indented) brackets (see my notes in the code below):
<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 -->
What's going on here? If my call to the _header.html.erb partial in application.html.erb is indented four spaces, do I need to indent the code in the partial by at least that same amount to have it nest properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
渲染的部分的第一行在 application.html.erb 中缩进为
<%= render 'layouts/header' %>
。但是所有其他代码行都不会进一步缩进,只是像您的部分代码一样左对齐。它也困扰着我,这也是我开始使用 haml 的部分原因。The first line of the partial that is rendered is indented as
<%= render 'layouts/header' %>
in application.html.erb. But all other lines of code are not indented further, just left-aligned as they are in your partial. It bugged me too, which is part of why I started using haml.