내 Rails 템플릿에서 HAML을 사용하여이 효과에 대한 최종 HTML을 완성하고 싶습니다.
I will first <a href="http://example.com">link somewhere</a>, then render this half of the sentence if a condition is met
가까이 오는 템플릿 :
I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
, then render this half of the sentence if a condition is met
그러나이 경우 링크와 쉼표 사이에 공백이 생깁니다. 이 공백을 피할 수있는 실용적인 방법이 있습니까? 태그 주위의 공백을 제거하는 구문이 있다는 것을 알고 있지만 동일한 구문을 텍스트에만 적용 할 수 있습니까? 이 작업을 수행하기위한 추가 마크 업 솔루션이 정말 마음에 들지 않습니다.
답변
이를 수행하는 더 좋은 방법은 Haml의 도우미를 통해 소개되었습니다.
둘러 싸다
= surround '(', ')' do
%a{:href => "food"} chicken
생성 :
(<a href='food'>chicken</a>)
성공 :
click
= succeed '.' do
%a{:href=>"thing"} here
생성 :
click
<a href='thing'>here</a>.
선행 :
= precede '*' do
%span.small Not really
생성 :
*<span class='small'>Not really</span>
원래 질문에 답하려면 :
I will first
= succeed ',' do
= link_to 'link somewhere', 'http://example.com'
- if @condition
then render this half of the sentence if a condition is met
생성 :
I will first
<a href="http://example.com">link somewhere</a>,
then render this half of the sentence if a condition is met
답변
Haml의 “trim whitespace”수정자를 사용하여이 작업을 수행 할 수도 있습니다. >
Haml 선언 뒤에 삽입 하면 공백이 추가되지 않습니다.
I will first
%a{:href => 'http://example.com'}> link somewhere
- if @condition
, then render this half of the sentence if a condition is met
생성 :
I will first<a href='http://example.com'>link somewhere</a>, then render this half of the sentence if a condition is met
그러나 보시 >
다시피 수정자는 링크 앞의 공백을 제거하여 단어와 링크 사이의 원하는 공백을 제거합니다.
“I will first”의 끝에 다음과 같이 추가 하는 것을 제외하고는 아직이 문제를 해결할 방법을 찾지 못했습니다 .
I will first
%a{:href => 'http://example.com'}> link somewhere
- if @condition
, then render this half of the sentence if a condition is met
마지막으로 읽기 어려운 보간없이 원하는 출력을 생성합니다.
I will first <span><a href="http://example.com">link somewhere</a></span>, then render this half of the sentence if a condition is met
답변
좋아, 내가 정착하고있는 해결책은 다음과 같다.
돕는 사람
def one_line(&block)
haml_concat capture_haml(&block).gsub("\n", '').gsub('\\n', "\n")
end
전망
I will first
- one_line do
= link_to 'link somewhere', 'http://example.com'
- if @condition
, then render this half of the sentence
\\n
if a condition is met
이렇게하면 공백이 기본적으로 제외되지만 “\ n”줄로 명시 적으로 포함 할 수 있습니다. (그렇지 않으면 HAML이 실제 개행으로 해석하기 때문에 이중 백 슬래시가 필요합니다.) 더 나은 옵션이 있는지 알려주세요!
답변
HAML의 ‘악어 구문’을 사용할 수 있습니다.
공백 제거 :> 및 <
및 <태그 근처의 공백을 더 잘 제어 할 수 있습니다. >는 태그 주변의 모든 공백을 제거하고 <는 태그 내의 모든 공백을 즉시 제거합니다. 공백을 먹는 악어라고 생각할 수 있습니다.>는 태그 바깥 쪽을 향하고 바깥 쪽의 공백을 먹고, <얼굴을 태그 안쪽에 있고 안쪽의 공백을 먹습니다. 태그 정의의 끝, 클래스, ID 및 속성 선언 뒤, / 또는 = 앞에 배치됩니다.
http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_
답변
이런 종류의 접근 방식을 취하면 문자열 보간을 사용하는 것입니다.
I will first #{link_to 'Link somewhere'}#{', then render this half of the sentence if a condition is met' if condition}
보간에서 리터럴 문자열의 모양이 마음에 들지 않지만 이전에 선언 된 문자열이나 동적으로 생성 된 문자열과 함께 사용했습니다.
답변
이렇게하면 선행 공백을 유지할 수 있습니다.
%a{:href => 'http://example.com'}>= ' link somewhere'
공백은 따옴표 안에 있습니다.
답변
잘 문서화되어 있지는 않지만 HAML 공백 보존 (>)과 ASCII 공백 (& # 32;)을 사용하여 명확하게 달성되며 도우미는 사용하지 않습니다.
%a{:href=>'/home'}> Home link
, 
%a{:href=>'/page'} Next link
이렇게하면 원하는 것이 생성됩니다.
<a href='/home'>Anchor text</a>, 
<a href='/page'>More text</a>
그러나 저는 동의합니다. HAML은 불필요한 ASCII 문자를 페이지에 추가하기 때문에 더 나은 방법을 찾아야합니다 (하지만 헬퍼를 사용하는 것보다 여전히 더 효율적입니다).
