、あなたは簡単に付属のコード・ブロックにメソッドの内部から変数を渡すことができます。mixin宣言の内側から変数を添付されたコンテンツブロックに渡しますか? Rubyでは
def mymethod
(1..10).each { |e| yield(e * 10) } # Passes a number to associated block
end
mymethod { |i| puts "Here comes #{i}" } # Outputs the number received from the method
私はSASSのミックスインで同じことをしたいと思います:
このコードウォン$ iはmixin宣言の中で宣言され、mixinが使われているところでは見えないので、うまくいきません。 :(
だから... ...どのように私はミックスイン宣言内で宣言された変数を利用していますか?
私はグリッドフレームワークとメディアクエリを使用する場合、私はひどく、この機能を必要としています。現在、私は内部で何複製する必要がありミックスイン宣言私はDRY原則に違反し、それを必要とするたびに。
UPDここでは2013年1月24日
は実際の例です。
私はmixiのを持っていますブレークポイントを循環は、すべてのブレークポイントのために一度提供されたコードを適用し、N:
=apply-to-each-bp
@each $bp in $bp-list
+at-breakpoint($bp) // This is from Susy gem
@content
私は@content内でこの$のBP値を使用する必要があり、このミックスインを使用
。// Applies to all direct children of container
.container > *
display: inline-block
// Applies to all direct children of container,
// if container does not have the .with-gutters class
.container:not(.with-gutters) > *
+apply-to-each-bp
width: 100%/$bp
// Applies to all direct children of container,
// if container has the .with-gutters class
.container.with-gutters > *
+apply-to-each-bp
$block-to-margin-ratio: 0.2
$width: 100%/($bp * (1 + $block-to-margin-ratio) - $block-to-margin-ratio)
width: $width
margin-right: $width * $block-to-margin-ratio
&:nth-child(#{$bp})
margin-right: 0
$ bpの値は@contentの中で利用できないので、これは機能しません。
mixinを呼び出す前に変数を宣言することは役に立ちません。なぜなら、@ contentは一度解析され、mixinが解析される前です。
代わりに、私は2つの醜い太ももをしなければならない、という私が必要とするたび: :
- は
- はDRY原則に違反し、サイクルを書き、アドホックのmixinを宣言します
// Each of the following mixins is mentioned in the code only once. =without-gutters($bp) width: 100%/$bp =with-gutters($bp) $block-to-margin-ratio: 0.2 $width: 100%/($bp * (1 + $block-to-margin-ratio) - $block-to-margin-ratio) width: $width margin-right: $width * $block-to-margin-ratio &:nth-child(#{$bp}) margin-right: 0 // Applies to all direct children of container .container > * display: inline-block // Applies to all direct children of container, // if container does not have the .with-gutters class .container:not(.with-gutters) > * @each $bp in $bp-list +at-breakpoint($bp) // This is from Susy gem +without-gutters($bp) // Applies to all direct children of container, // if container has the .with-gutters class .container.with-gutters > * @each $bp in $bp-list // Duplicate code! :( +at-breakpoint($bp) // Violates the DRY principle. +with-gutters($bp)
質問:このRubyスタイルを行う方法はありますか?
親愛なるcimmanonさん、あなたが提案した解決策は、この状況には適用されません。私は私の最初の質問を編集して、私が探しているものの徹底的で実際的な例を追加しました。見て、あなたの考慮事項を書いてください。 –