Ruby, kolayca ekli kod bloğuna bir yöntem içinden bir değişken geçirebilirsiniz:Bir mixin bildiriminin içinden ekli içerik bloğuna bir değişken geçiriliyor mu?
=my-mixin
@for $i from 1 to 8
.grid-#{$i}
@content
+my-mixin
color: nth("red green blue orange yellow brown black purple", $i)
Bu kod kazandı:
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
Ben SUKDÖ'nün mixin aynı şeyi yapmak istiyorum işe yaramaz çünkü mixin bildirimi içinde $ i beyan edilir ve karışımın kullanıldığı yerlerde dışarıdan görülemez. i mixin bildiriminde bildirilebilir değişkenleri kaldıraç nasıl :(
So ...?
i ızgara çerçevesi ve medya sorgu ile çalışmak, ben Şu ı içinde ne çoğaltmak zorundayız. kötü bu işlevselliği gerek mixin deklarasyon i KURU kuralını ihlal lüzum her zaman.
UPD İşte 2013-01-24
gerçek hayatta örnek.
bir mixi var kesme noktaları aracılığıyla döngüleri ve her kesme noktası için bir kez verilen kodu geçerlidir n:
=apply-to-each-bp
@each $bp in $bp-list
+at-breakpoint($bp) // This is from Susy gem
@content
i @content içinde bu $ bp değeri kullanmak zorunda bu mixin kullanmak
. Böyle olabilir:// 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
Ama $ bp değeri mevcut iç @content olmadığı için bu, işe yaramaz.
Karışımı çağırmadan önce değişkeni bildirmek yardımcı olmaz çünkü @content bir kez ayrıştırılır ve mixin ayrıştırılmadan önce.
- geçici bir mixin beyan,
- , döngüsünü yazmak KURU ilkesini ihlal:
Bunun yerine, ben bu ihtiyaç HER zaman, ben iki çirkin uyluk yapmak zorunda
// 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)
Yani, soru şu: Bu Ruby stilini yapmanın bir yolu var mı?
Sevgili cimmanon, önerdiğiniz çözüm bu durum için geçerli değildir. İlk soruyu, aradığım şeyin tam ve neredeyse gerçek hayattaki bir örneğini eklemek için düzenledim. Lütfen bir göz atın ve düşüncelerinizi yazın. –