質問

私の 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の「トリム空白」修飾子を使用してこれを行うことができます。 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&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は実際の改行としてそれを解釈するので、それは二重のバックスラッシュを必要とします。)そこに良いオプションがありますなら、私を知ってみましょう!

あなたはHAMLの 'aligator構文' を使用することができます。

  

空白除去:>と<

     

と<はあなたに、タグの近くに空白をより細かく制御を与えます。 <タグ内で、すぐにすべての空白を削除する一方>は、タグの周囲のすべての空白を削除します。あなたは空白を食べるワニと考えることができます:>タグの外に面しており、外側には空白を食べ、そして<タグに面しており、内側には空白を食べます。彼らは、クラス、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空白の保存を使用して達成される(>)(&#32;)ASCII空間と合わせ、そしていないヘルパーと

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

これは、あなたが望むものを生成します。

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

しかし、私はそれがページに不要なASCII文字を追加しないようHAMLは、これを行うためのより良い方法を考え出す必要があります(それはまだヘルパーを使用するよりも効率的だ)、同意します。

アングルブラケット「空白むしゃむしゃ」構文があります、それ以外の場合のためのヘルパーメソッドを記述します。

私は同様の問題に出くわしたので、私はヘルパーメソッドを必要としない別の解決策を投稿するだろうと思った本を見つけました。

:リンクとif文をラップするために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のコードの結果を用いているが、

あなたは、=を使用することができますが、ここでは、サーバう目的ます。

scroll top