문제

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

그러나 보시다시피 > 수정자는 또한 링크 앞에서 공백을 제거하여 단어와 링크 사이의 원하는 공간을 제거합니다. 나는 이것을 추가하는 것을 제외하고는 아직 이것에 대해 예쁜 방법을 찾지 못했습니다. &nbsp; "I Will First"가 끝날 때까지 :

I will first&nbsp;
%a{:href => 'http://example.com'}> link somewhere
- if @condition
  , then render this half of the sentence if a condition is met

마지막으로 읽기 어려운 보간없이 원하는 출력을 생성합니다.

I will first&nbsp;<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이 실제 Newline으로 해석하기 때문에 Double-Backslash가 필요합니다.) 더 나은 옵션이 있는지 알려주십시오!

Haml의 'Aligator Syntax'를 사용할 수 있습니다.

공백 제거 :> 및

그리고 <태그 근처의 공백을 더 많이 제어 할 수 있습니다. > 태그 주변의 모든 공백을 제거하고 <는 태그 내에서 즉시 모든 공백을 제거합니다. 당신은 그것들을 공백을 먹는 악어로 생각할 수 있습니다. 그들은 클래스, 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'

공간은 인용문에 있습니다.

잘 문서화되지는 않았지만, 이것은 ASCII 공간 (& #32;)과 결합 된 HAML 공백 보존 (>)을 사용하여 깨끗하게 달성됩니다.

%a{:href=>'/home'}> Home link
,&#32; 
%a{:href=>'/page'} Next link

이것은 당신이 원하는 것을 생산할 것입니다.

<a href='/home'>Anchor text</a>,&#32;
<a href='/page'>More text</a>

그러나 Haml은 페이지에 불필요한 ASCII 문자를 추가하기 때문에 더 나은 방법을 제시해야합니다 (그러나 도우미를 사용하는 것보다 여전히 더 효율적입니다).

Angle Bracket "Whitespace Munching"구문이 있습니다. 그렇지 않으면 도우미 방법을 작성하십시오.

나는 비슷한 문제를 발견하고 이것을 발견하여 도우미 방법이 필요하지 않은 다른 솔루션을 게시 할 것이라고 생각했습니다. Ruby 보간 #{}를 사용하여 링크를 마무리하고 문장을 마무리하십시오.

I will first 
#{link_to 'link somewhere', 'http://example.com'}#{if true : ", then render this half of the sentence if a condition is met" end}

이것은 3.0.18에서 작동하며 이전 릴리스에서도 작동 할 수도 있습니다.

내가 과거에 사용한 또 다른 옵션 :

- if @condition
  %span> , then some more text after the link.

당신은 또한 항상 할 수 있습니다 :

= link_to url_path do 
  = ["part_1", "part_2"].join(", ")

내가 일한 솔루션은 다음과 같습니다.

I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
  = ", then render this half of the sentence if a condition is met"

당신이 사용할 수있는 =, 그렇지만 = Rails 코드의 결과를 출력하는 데 사용되지만 여기서는 목적을 서버합니다.

그만큼 보존하다 기능이 저를 위해 작동했습니다

.white-space-pre= preserve "TEXT"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top