रूबी में, आप आसानी से एक चर एक विधि के अंदर से जुड़ी कोड ब्लॉक में पारित कर सकते हैं:संलग्न सामग्री ब्लॉक में मिश्रित घोषणा के अंदर से एक चर पारित करना?
=my-mixin
@for $i from 1 to 8
.grid-#{$i}
@content
+my-mixin
color: nth("red green blue orange yellow brown black purple", $i)
इस कोड को जीता:
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
मैं एस.ए.एस.एस. mixin में एक ही बात करना चाहते हैं काम नहीं करते क्योंकि $ i को मिक्सीन घोषणा के अंदर घोषित किया गया है और बाहर नहीं देखा जा सकता है, जहां मिश्रण का उपयोग किया जाता है। :(
तो ... मैं कैसे चर mixin घोषणा के अंदर घोषित लाभ उठाते हैं?
जब मैं एक ग्रिड ढांचे और मीडिया के प्रश्नों के साथ काम करते हैं, मैं बुरी तरह से इस कार्यक्षमता की जरूरत है। वर्तमान में मैं अंदर क्या है नकल करने के लिए है mixin घोषणा हर बार मैं इसे की जरूरत है, सूखी नियम का उल्लंघन करने।
युपीडी 2013-01-24
यहाँ एक वास्तविक जीवन उदाहरण है।
मैं एक mixi है n कि breakpoints के माध्यम से चक्र और हर ब्रेकप्वाइंट के लिए एक बार प्रदान की कोड लागू होता है:
=apply-to-each-bp
@each $bp in $bp-list
+at-breakpoint($bp) // This is from Susy gem
@content
जब मैं इस mixin मैं @content अंदर इस $ बीपी मान का उपयोग करने के लिए है का उपयोग करें। यह इस तरह हो सकता है:
// 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
लेकिन यह काम नहीं करेगा, क्योंकि $ बीपी का मूल्य @content के अंदर उपलब्ध नहीं है।
मिश्रण को कॉल करने से पहले चर को घोषित करने से मदद नहीं मिलेगी, क्योंकि @content को एक बार और पहले मिलाकर मिलाया जाता है।
- एक तदर्थ 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)
तो, सवाल यह है: क्या इस रूबी शैली को करने का कोई तरीका है?
प्रिय सिममन, आपके द्वारा सुझाए गए समाधान स्थिति पर लागू नहीं होते हैं। मैंने अपने शुरुआती प्रश्न को संपादित करने के लिए एक पूर्ण और लगभग वास्तविक जीवन उदाहरण जोड़ने के लिए संपादित किया है। कृपया एक नज़र डालें और अपनी विचार लिखें। –