code

erb, haml 또는 slim : 어떤 것을 제안합니까?

codestyles 2020. 8. 23. 09:14
반응형

erb, haml 또는 slim : 어떤 것을 제안합니까? 그리고 왜? [닫은]


저는 Rails를 배우고 있으며 이러한 템플릿 엔진을 보았습니다. 나는 그들에 대한 경험이 없습니다 (단지 erb).

근데 초심자라서 정말 헷갈려요. 어떤 것을 제안하고 그 이유는 무엇입니까? Erb, Haml 또는 Slim? 다른 것보다 선호하는 이유를 알려주십시오. 다른 권장 사항이 있으면 알려주십시오.

편집 : 나는 여기서 우승자를 찾고 있지 않습니다. 나는 그들, 구문, 실행 속도 등에 대한 귀하의 의견을 듣고 싶습니다.


ERB는 주로 일반 HTML에서 작업 할 웹 디자이너가 있고 햄 또는 슬림을 알지 못하는 경우에 좋습니다. 이런 식으로 그는 HTML을 작성할 수 있고 적절한 태그를 사용하여 루비 로직을 포함 할 수 있습니다.

HTML과 루비 로직을 모두 작업하거나 디자이너가 HAML과 같은 새로운 것을 배울 준비가되어 있다면 HAML을 선택하겠습니다. 루비 친화적이고 ERB보다 훨씬 더 읽기 쉽고 문자 수를 줄입니다.

예 (공식 HAML 사이트 에서 가져옴 ) :

ERB에서보기는 다음과 같습니다.

<div id="profile">
  <div class="left column">
    <div id="date"><%= print_date %></div>
    <div id="address"><%= current_user.address %></div>
  </div>
  <div class="right column">
    <div id="email"><%= current_user.email %></div>
    <div id="bio"><%= current_user.bio %></div>
  </div>
</div>

HAML에서는 다음과 같이 보입니다.

#profile
  .left.column
    #date= print_date
    #address= current_user.address
  .right.column
    #email= current_user.email
    #bio= current_user.bio

훨씬 더 깨끗해!

HAML과 SLIM의 차이점은 SLIM으로 작업 한 적은 없지만 취향의 문제라고 생각합니다. 두 구문을 모두 살펴보고 어느 것이 더 잘 보이는지 결정하십시오. 이 둘 (HAML / SLIM) 사이에 확실한 승자가 있다고 생각하지 않습니다.


햄보다 슬림을 사용하는 두 가지 큰 장점 :

  1. Slim은 현재 haml보다 약 8 배 빠릅니다.

  2. Slim은 HTTP 스트리밍을 지원하지만 HAML은 지원하지 않습니다.

  3. Slim은 더 자연스러운 구문을 가지고 있습니다. a href="foo.html"


내 머리 꼭대기에서 이것이 내가 생각 해낸 것입니다.

ERB :

장점

  • 기본 제공
  • 공백에 의존하지 않음
  • Ruby 코드가 뿌려진 HTML로 가장 낮은 진입 장벽 (HTML에서 오는 경우)
  • 대부분의 IDE의 렉서는 기본적으로 읽습니다.
  • DHH는 그것을 선호합니다
  • 레거시 앱이 여전히 사용 중

단점

  • 더 자세한
  • 헬퍼 및 뷰의 content_for 태그는 빠르게 벗어날 수 있습니다.
  • content_for 태그는 erb가 블록의 마지막 줄만 반환하므로 중첩 태그를 더 어렵게 만듭니다. 따라서 문자열에 추가 한 다음 반환해야합니다.

HAML

장점

  • 더 간결합니다. 닫는 태그 없음, 작은 화면에 적합
  • 시각적으로 깔끔한 구조
  • 헬퍼 메소드에서 haml을 활용하기 위해 헬퍼 (haml_concat, haml_capture)가 내장되어 있습니다.
  • 클래스 체인
  • # for divs 또는. 클래스 체이닝의 경우, JS 태그의 경우 : javascript

단점

  • whitespace dependent which makes for some hard errors to figure out at times
  • complex tags usually need to resort to "hash" format. (Although I actually think this is a great example of flexibility to someone starting out it could be a pain.)
  • added as a gem (again probably a stretch to put this as a con)
  • designers might have some trouble adjusting
  • in addition to the general whitespace warning... simple whitespace errors eg. tabs and spaces for indentation, can cause pages to err in production which normal specs/test won't catch. Moral: Expect greater need for view tests and possibly don't use haml for mission critical views, unless you're sure that your tests are testing the actual rendering of the view.
  • is slower (than erb)
    • caveat: this is ruby code we're talking about if speed is a blocking issue in your application there are alternatives to ruby, e.g. haskell

The question for me comes down to would you rather put % before every tag or | before every new block of text?

Slim:

 tag(attr= "value")
  | text

Haml:

 %tag{attr: "value"}
   text

One more thing to lookout for: haml assumes a white space between new lines (remove whitespace in haml) while slim assumes no space (Add whitespace in Slim here and here)


https://github.com/scalp42/hamlerbslim - is an independent benchmark which shows Slim and Erb as winners, performance wise (slim tends to reduce the HTML output size too.)

My personal opinion is that overall, Slim and Haml will save you time (== money) in terms of maintenance, providing you have Haml/Slim savvy people looking after your views.

If you don't have those people, Erb is definitely the way to go, because despite the best will in the world, there are a lot of very inexpensive people available who can work with HTML/Erb, but find Haml/Slim a complete mystery.

Best of all cases, train these people to use Slim or at least expose them to it, and keep the numbers of the ones who "get it."

참고URL : https://stackoverflow.com/questions/11390512/erb-haml-or-slim-which-one-do-you-suggest-and-why

반응형

'code' 카테고리의 다른 글

HQL에서 고유 한 쿼리를 생성하는 방법  (0) 2020.08.23
Bash의 문자열 차이  (0) 2020.08.23
Ruby의 문자열에서 문자 # 개 찾기  (0) 2020.08.23
CSS 진행 서클  (0) 2020.08.23
단일 Git 커밋 리베이스  (0) 2020.08.23