Html 2 个空格或 1 个制表符,Rails 社区中的缩进标准是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2678817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:46:54  来源:igfitidea点击:

2 Spaces or 1 Tab, what's the standard for indentation in the Rails Community?

htmlruby-on-railsxmlindentationhaml

提问by Lance Pollard

I've noticed that most of the HTML/XML/HAML that gets generated from plugins uses 2 spaces instead of 1 tab. I use textmate and have tabs set to 4 spaces for HAML/HTML/XML and 2 spaces for Javascript/Ruby, but I only have to press the tab key once to get nice indentation. Pressing the space bar twice and delete twice seems like too much work :p.

我注意到大多数从插件生成的 HTML/XML/HAML 使用 2 个空格而不是 1 个制表符。我使用 textmate 并将制表符设置为 HAML/HTML/XML 的 4 个空格和 Javascript/Ruby 的 2 个空格,但我只需要按一次 Tab 键即可获得良好的缩进。按两次空格键并删除两次似乎工作量太大:p。

Do you manually type two spaces, or is some middle layer converting tabs to two spaces? Or do just a few of you use tabs?

您是手动输入两个空格,还是某个中间层将制表符转换为两个空格?还是只有少数人使用标签?

回答by Ben

2 spaces is generally agreed-upon.

2个空格是普遍同意的。

回答by Mike

As for all the arguments about different editors showing tabs with different widths...

至于关于不同编辑器显示不同宽度的标签的所有论点......

It is a configuration setting people!

它是一个配置设置的人!

Change your editors to use two spaces for each tab. Different widths in different editors is not an argument. It is a moot point. If anything, using tabs allows people who prefer different widths to set and use the width they prefer. Hell, I think I like 3 spaces.

将您的编辑器更改为每个选项卡使用两个空格。不同编辑器中的不同宽度不是一个参数。这是一个有争议的问题。如果有的话,使用制表符允许喜欢不同宽度的人设置和使用他们喜欢的宽度。见鬼,我想我喜欢 3 个空格。

And you have to hit backspace twice just to un-indent something once. I'm a programmer, I'm lazy. I don't like to do things more than once!

而且您必须按两次退格键才能取消缩进一次。我是程序员,我很懒。我不喜欢做不止一次的事情!

回答by ma?ek

2 spaces. Even for the plain Ruby kids.

2个空格。即使是普通的 Ruby 孩子。

回答by Brandon Rhodes

Two spaces are emerging as the standard because they are “just enough” indentation to make the HTML clearly indented to most people's eyes, but because HTML tends to nest very deeply — much more deeply than one would commonly nest when programming — and anything more than two spaces tends to start pushing HTML off the right edge of an 80-column screen pretty quickly.

两个空格正在成为标准,因为它们是“刚好”的缩进,可以使 HTML 在大多数人眼中清晰地缩进,但因为 HTML 往往嵌套得非常深——比编程时通常嵌套的要深得多——而且任何东西都超过两个空格往往会很快将 HTML 推离 80 列屏幕的右边缘。

Do not use tabs; use two spaces. Tabs are problematic because they can be a different width in different editors (and terminals, for that matter). Since whitespace is removed when you tidy your HTML before deployment, there is no ultimate bandwidth benefit to using tabs; they simply introduce a wildcard into your development experience.

不要使用标签;使用两个空格。标签是有问题的,因为它们在不同的编辑器(和终端,就此而言)可以有不同的宽度。由于在部署前整理 HTML 时会删除空格,因此使用选项卡没有最终的带宽优势;他们只是在您的开发体验中引入了一个通配符。

I use Emacs, which auto-indents XML, HTML, and SGML with two spaces. I would be quite annoyed if I had to type indentation characters by myself; that's why I have an editor. :)

我使用 Emacs,它使用两个空格自动缩进 XML、HTML 和 SGML。如果我必须自己输入缩进字符,我会很生气;这就是为什么我有一个编辑器。:)

回答by Sean Tan

2 spaces is the convention for Ruby which Ruby on Rails is based upon.

2 个空格是 Ruby on Rails 所基于的 Ruby 约定。

回答by konung

In more practical terms, if you use mutliple OS and editors ( such as at work I used RubyMine on Win XP and TextMate on my MacBook at home and one of our developers uses Notepad ++ and another Aptana and we even have one who uses Netbeans, all of these editors interpret tabs differently - different indentation, but 2 spaces are always 2 spaces. Also if you ever use HAML or if you need to copy paste your code into some websites that try to colorcode your code, 2 spaces are essential.

更实际地说,如果您使用多个操作系统和编辑器(例如在工作中我使用 Win XP 上的 RubyMine 和家里 MacBook 上的 TextMate,我们的一个开发人员使用 Notepad ++ 和另一个 Aptana,我们甚至有一个使用 Netbeans ,所有这些编辑器都以不同的方式解释制表符 - 不同的缩进,但 2 个空格始终是 2 个空格。此外,如果您使用过 HAML,或者如果您需要将代码复制粘贴到一些尝试对代码进行颜色编码的网站中,2 个空格是必不可少的。

回答by TheCrazyProfessor

For HTML & CSS look at this.

对于 HTML 和 CSS,看看这个。

I recommend that you always check up the individual programing language before coding after they use different standards

我建议您在编码之前始终检查各个编程语言,因为它们使用了不同的标准

E.g Python is using 1 tab(4 spaces) No matter what, while the recommend for HTML & CSS is 2 spaces

例如,Python 使用 1 个制表符(4 个空格)无论如何,而对 HTML 和 CSS 的推荐是 2 个空格