ã¯ããã«
ãã®èšäºã§ã¯ãééãããžã§ã¯ãã®BEMã¬ã€ã¢ãŠãææ³ã«ã€ããŠã話ããããšæããŸãã ç§ã¯ãŸã ãã®ãããªããã¥ã¢ã«ãèŠãŠããŸããïŒå€åãããé€ããŸãããã¬ã€ããšããŠã¯ããŸãæ©èœããªãã®ã§ãåŸã§è©³ãã説æããŸãïŒããã®èšäºãæžãããšã«ããŸããã ããã«ãBEMãšã¬ãŒã«ã®çµ±åãç°¡çŽ åããgemãäœæããŸãããããã«ã€ããŠã¯åŸã§èª¬æããŸãã
æºåãã
ãŸããå®éã®æ©æŠãšã¬ãŒã«ãåãä»ããå¿ èŠããããŸãã ç§ã¯rvmã奜ãã§ã ã rvmã®ã€ã³ã¹ããŒã«æé ã«åŸããã³ãã³ãã䜿çšããŠã¬ãŒã«ãã€ã³ã¹ããŒã«ããŸãã
gem install rails
ããŠãWindowsããæã¡ã®å Žåã¯ãããããã¬ãŒã«ãã€ã³ã¹ããŒã«ã§ããŸãã
次ã«ãã³ãã³ãã䜿çšããŠæ°ãããããžã§ã¯ããäœæããŸãã
rails new some_cool_project
次ã«ãæ°ããäœæããããããžã§ã¯ãã®ã«ãŒãã«ããGemfileãã¡ã€ã«ã«ãbemãgemãè¿œå ããŸãã ãŸãã high_voltageããã³slim-rails gemãè¿œå ããããšããå§ãããŸãã 1ã€ç®ã¯ãã¬ãŒã«ãšããã¯ã«é¢ããç¹å¥ãªç¥èããªããŠããã¬ã€ã¢ãŠãèšèšè åãã®æšæºããŒãžãäœæã§ããŸãã2ã€ç®ã¯ãã¬ã€ã¢ãŠããå€§å¹ ã«é«éåããåºåHTMLãçž®å°ã§ããåªãããã³ãã¬ãŒããšã³ãžã³ã§ãã
次ã®ã³ãã³ãã䜿çšããŠãbem gemã«å¿ èŠãªãã¡ã€ã«ãã€ã³ã¹ããŒã«ããŸãã
rails g bem:install
config / initializers / bem.rb configãã€ã³ã¹ããŒã«ãããŸããããã§ã¯ããã¯ãããžãŒïŒå ã ã¯scssãšjsïŒãšããã¯ãããžãŒããšã®ãã¡ã€ã«ãçæããããã®ãã³ãã¬ãŒããå€æŽã§ããŸãlib / bem / templates / scss.ttãšlib / bem / templates / js.tt ã ãããžã§ã¯ãã§ããŸã䜿çšããããªãå Žåããã®ãã¯ãããžãŒã¯scssã§ã¯ãªãconfig / initializers / bem.rb configã§èšè¿°ããå¿ èŠããããŸãïŒ
BEM.configure do |config| config.technologies = [ { :group => 'stylesheets', :extension => '.less', :name => 'less', :css_directive => '@import', :css_prefix => "'", :css_postfix => "';" }, { :group => 'javascripts', :extension => '.js', :name => 'js' } ] end
次ã®ã³ãã³ãã䜿çšããŠã springã®èšå®ãã€ã³ã¹ããŒã«ããããšã匷ããå§ãããŸãã
rails g bem:spring
ãã®ã©ã€ãã©ãªã䜿çšãããšãããã¯ã°ã©ãŠã³ãã§ç°å¢ãããŒãã§ãããããã¯ããã°ããçæã§ããŸãã
springã®æ§æãã€ã³ã¹ããŒã«ããå Žåã次ã®ã³ãã³ããå®è¡ããŸãã
spring stop
ãã®ã³ãã³ãã¯ãSpringãåŸã§åèµ·åããããã«å¿ èŠã§ãã
ããã§ãã¬ã€ã¢ãŠããéå§ã§ããŸãã
ã¬ã€ã¢ãŠã
ã¬ã€ã¢ãŠãã®äŸãšããŠã ãã®èšäºããã¬ã€ã¢ãŠããéžæããŸããã
ãããã¯ãã¬ãã«ãããã³ãããã§ã¹ãã¯ãbem createã³ãã³ãã䜿çšããŠäœæãããŸãã ãã®ã³ãã³ãã¯ã次ã®ãã©ã°ãå ¥åãšããŠåãå ¥ããŸãïŒãã©ã°ã®å€ã¯ããããã®åŸã«ã¹ããŒã¹ãä»ããŠæå®ããå¿ èŠããããŸãïŒã
- -bã¯ããããã¯ãäœæãŸãã¯äœ¿çšããŸãã
- -eã¯ãèŠçŽ ãäœæãŸãã¯äœ¿çšããŸãã
- -mã¯ä¿®é£ŸåãäœæããŸãã
- -v修食åã®å€ã
- -lã¯ã¬ãã«ãäœæãŸãã¯äœ¿çšããŸãã
- -aãããã§ã¹ããäœæãŸãã¯äœ¿çšããŸãã
- JavaScriptãã¯ãããžãŒãã¡ã€ã«ãäœæããããã®-jsãã©ã°ã
- ã¹ã¿ã€ã«ã·ãŒããã¯ãããžãŒãã¡ã€ã«ãäœæããããã®-cssãã©ã°ã
ã³ãã³ããå®è¡ããŠããã©ã°ãšãã®äœ¿çšã«é¢ããæ å ±ãèªã¿åãããšãã§ããŸãã
bem help create
createã³ãã³ãã¯ãåããã¡ã€ã«æ§é ãäœæããŸãïŒ ãã¡ããšåãïŒã
次ã«ããããã¯ãšãããã§ã¹ããã³ãã³ããä»ããŠæ¥ç¶ãããå°æ¥ã®ã¬ãã«ãäœæããŸãã
spring bem create -l shared -a application
ã°ããªãã§ãå¯èœã§ãã
bem create -l shared -a application
ããããç§ãèšã£ãããã«ãæ¥ã¯ç°å¢ãããã¯ã°ã©ãŠã³ãã«ä¿ã¡ãåŸç¶ã®ã³ãã³ãã®å®è¡ãé«éåããŸãã
ãã®ã³ãã³ããå®è¡ãããšã䜿çšãããŠããåãã¯ãããžãŒã«å¯ŸããŠã¬ãã«ãšãããã§ã¹ããäœæããããšããã¡ãã»ãŒãžãã¿ãŒããã«ã«è¡šç€ºãããŸãã ãã®å Žåãå ±æã¯ãããã¯ãé 眮ãããã¬ãã«ã§ãããã¢ããªã±ãŒã·ã§ã³ã¯ãããã§ã¹ãã§ãã
ãããã§ã¹ããã¡ã€ã«ãèŠããšãå ±æã¬ãã«ãæååã§æ¥ç¶ãããŠããããšãããããŸãã
@import 'shared/shared';
åçŽãªã«ãŒã«ãé©çšãããŸã-ã¬ãã«ã¯ãããã§ã¹ãã§æ¥ç¶ããããããã¯ã¯ã¬ãã«ã§æ¥ç¶ãããŸãã
ãããã§ã¹ãã¢ããª/è³ç£/application.lessã䜿çšããããã以åã®ãããã§ã¹ãã¢ããª/è³ç£/application.cssãåé€ããŸãã
次ã«ãåæ§ã®ã³ãã³ãã§å¿ èŠãªãããã¯|èŠçŽ |修食åãäœæããŸãã
spring bem create -l shared -b clear --no-js spring bem create -l shared -b page --no-js spring bem create -l shared -b page -e head-line --no-js spring bem create -l shared -b page -e line --no-js spring bem create -l shared -b link -m menu -v active --no-js ...
ã芧ã®ãšãããjsãã¡ã€ã«ã¯ãããã®ãããã¯ã«ã¯äœ¿çšãããªãããããããã¯ãé 眮ãããå ±æã¬ãã«ãš--no-jsãã©ã°ã瀺ããŸãã
ãããã®ã³ãã³ããå®è¡ãããšãå¿ èŠãªcssã¯ã©ã¹ãæ¢ã«ç»é²ãããŠãããã³ãã¬ãŒããäœæãããå¿ èŠãªããšã¯é©åãªããããã£ãå ¥åããã ãã§ãã
ééããŠééã£ãã¯ã©ã¹ãæžããããééã£ãã¬ãã«/ãããã§ã¹ãã«ãããã¯ãå«ãããããå Žåã¯ããªããŒã¹ã³ãã³ããå®è¡ããŠãbem destroyã³ãã³ãã§ãããã¯|èŠçŽ |修食å|ã¬ãã«ãåé€ããããšã§ãã€ã§ãããŒã«ããã¯ã§ããŸãã --jsããã³--cssãé€ããã¹ãŠã®åããã©ã°ãåãå ¥ããŸãã
次ã«ãå¿ èŠãªãããã¯|èŠçŽ |修食åãäœæãããšãhtmlã¬ã€ã¢ãŠãã«ç§»åããŸãã æ¢ã«èª¬æããhigh_voltage gemãã€ã³ã¹ããŒã«ããå Žåã¯ããã©ã«ããŒãäœæããã ãã§ååã§ãã
mkdir app/views/pages
ã©ã®ãã¥ãŒã«å¿ èŠãªãã¥ãŒãé 眮ãããŸãã ãã®gemã®ããã¥ã¡ã³ããèªããšããããããã¥ãŒã®æ¥ç¶ãšè¡šç€ºã®ãããããæ°ã«å ¥ã£ãŠããã§ãããã ããšãã°ãããããããŒãžã®ã¬ã€ã¢ãŠããå¿ èŠã§ãã app / views / pages / welcome.html.slimãã¥ãŒãäœæããããã«å¿ èŠãªhtmlãèšå®ããŸãã ã³ãã³ãã䜿çšããŠWebãµãŒããŒãèµ·åããåŸ
rails s
çµæã¯httpïŒ// localhostïŒ3000 / pages / welcomeã§ç¢ºèªã§ããŸã
ãã¹ãŠã®åçãapp / asset / imagesãã©ã«ãã«å ¥ãããã¹ãŠã®ã¿ã°ãimage_tagã¬ãŒã«ãã«ããŒã«çœ®ãæããŸãã lessãã¡ã€ã«å ã®ç»åã®ã¢ãã¬ã¹ã¯image-urlïŒless-rails gemãæäŸãããã«ããŒïŒã«çœ®ãæããããŸãã
æ®å¿µãªãããäžèšã®äŸã§ã¯jsã䜿çšããŠããŸããã ããã«ãããããããæäŸãããŠãããã³ãã¬ãŒãã«ãããç®çã®é¢æ°ãç°¡åã«äœæããŠå®è¡ã§ããŸãã
function your_function_name_initializer() { // some code } $(function() { your_function_name_initializer(); }); $(window).bind('page:load', function() { your_function_name_initializer(); })
ãªããã å®éã«ã¯ãã¬ãŒã«ã®ããŒãžã§ã³4以éãã¿ãŒããªã³ã¯gemã䜿çšãããããŒãžã®è¡šç€ºãå€§å¹ ã«é«éåãããŸãããjsåæåã«ããã€ãã®æ©èœãè¿œå ãããããããããã®åæåãé¢æ°ãšããŠèšèšãããããã®2ã€ã®ã€ãã³ãã§åŒã³åºãå¿ èŠããããŸãã
転éãããã¬ã€ã¢ãŠãã®æçµããŒãžã§ã³ãšééãããžã§ã¯ãèªäœã¯ãgithubã§è¡šç€ºã§ããŸãã
代æ¿æ¡
æåã«è©±ããèšäºã«ã€ããŠã®bem-on-rails gemããããŸãã ç§ã¯ãã§ã«ããã䜿ãããã£ãã®ã§ãããçŸæç¹ã§ã¯å€ãã®äžå©ãªç¹ããããç§ã®å®ç³ãæžãããšãäœåãªããããŸããã ãã€ãã¹é¢ãšããŠã¯ãã¬ãã«ãšãããã§ã¹ãïŒéåžžãã¬ãã«ãšãããã§ã¹ãã§2ã€ã®ãããã§ã¹ããäœæããããšã¯ã§ããŸããã§ããïŒãèªã¿ã«ããã³ãŒãïŒã¿ããšããŠ4ã€ã®ã¹ããŒã¹ãã©ãã2ã€ïŒãããã³äžè¬ã«å質ãäœãããšã§ããã³ãŒãïŒåäžã®ãã¹ãã§ã¯ãªããã¢ãŒããã¯ãã£ãããgemãæ¥ãã§è¡ãããã³ãŒããããã«æã蟌ãŸããããã«æããäžè¬ã«ããŒãžã§ã³4.1ã®ã¬ãŒã«ã§ã¯å®è¡ã§ããŸããã§ãã- ããããéä¿¡ããå¿ èŠããããŸããïŒãã®å®ç³ã®ãã«ã㌠ãã®ãããªäŸïŒåçŽããã¿ïŒãåä¿¡ozhetïŒ
= b 'footer', content: [{ elem: 'el', elemMods: ['left'], content: [' «»'] }, { elem: 'el', elemMods: ['center'], content: [{ elem: 'nav-link', tag: 'a', content: [''] }, { elem: 'nav-link', tag: 'a', content: [' ']}, { elem: 'nav-link', tag: 'a', content: [' '] }] }, { elem: 'el', elemMods: ['right'], content: ['<a href="http://ya.ru/"> </a>'.html_safe] }]
ããã¯æããã«èªã¿ããããåäžããããã¬ã€ã¢ãŠãèšèšè ãæ··ä¹±ãããå¯èœæ§ããããŸãã
ããã§ããäžè¬çã«ããã¯bem-toolsãã¬ãŒã«ã«é©åãããè¯ãè©Šã¿ã ãšæããŸãã
ããŒããã€ããªã©ãgruntãä»ããŠãã€ãã£ãbem-toolsã䜿çšããããã«ãã¬ãŒã«ã«ããŒããåã蟌ãããã®ããã€ãã®ãšããŸããã¯ãªãªãã·ã§ã³ããããŸãã ãããããã®ãªãã·ã§ã³ã¯éåžžã«é¢åã§é£ãããšèãããããçå£ã«èããŠããŸããã§ããã
ãã¡ãããå¥ã®ãªããžããªã§bem-toolsèªäœã䜿çšããããšãã§ããŸããããã®å Žåã¯ã¬ã€ã¢ãŠããšè»¢éã®ããã»ã¹ãéåžžã«è€éã«ãªããŸããããšãã°ã絶ããã¬ã€ã¢ãŠãã远跡ãããªã©ãURLãèªåçã«è»¢éããã³çœ®æããããã®ïŒbashïŒã¹ã¯ãªãããèšè¿°ããå¿ èŠããããŸãååŸã«ã ãŸããããã¯å€§äžå€«ã§ãã ããããææªã®éšåã¯ãããã2ã€ã®ãªããžããªã§ãã©ã³ããããã©ã³ãã«åãæ¿ããããšã§ãã転éã¹ã¯ãªãããéå§ããåã«ãrailsã䜿çšãããªããžããªãšã¬ã€ã¢ãŠãã䜿çšãããªããžããªã§æ£ãããã©ã³ãã䜿çšããã©ã®ãã©ã³ããåªãããŠããŒãžããããã確èªããŠãã ãã圌女ãå¥ã®ãªããžããªã«ããŸãã ãããã£ãŠããœãªã¥ãŒã·ã§ã³ã¯æé©ã§ã¯ãããŸããã
ãããã«
ãããã£ãŠãã¬ã€ã¢ãŠããã¶ã€ããŒã«ãšã£ãŠã¯ãcss | jsã§ãã¥ãŒãšãããã¯ããªãããããã ãã§ãå éšã§äœãèµ·ãã£ãŠããããå¿ ãããç¥ãå¿ èŠã¯ãããŸããã ãããŠãããã¯ãšã³ãéçºè ãå¿ èŠãªããžãã¯ãæ¿å ¥ããŸãã ã€ãŸããã¬ã€ã¢ãŠãããã»ã¹ãå€§å¹ ã«ç°¡çŽ åãããŸãã
ç§ãæžããbemã©ã€ãã©ãªãŒïŒgemïŒã¯å®å šã«ãªãŒãã³ã§ãæãæé ãªMITã©ã€ã»ã³ã¹ã䜿çšããŠããŸãã ã©ã€ãã©ãªãæè¿äœæããŸãããããã£ãŒãããã¯ãšææ¡ãããã³githubã®ãã«ã¯ãšã¹ããåãã§åãåããŸãã
ç§ã¯ãã¹ãŠã«ã€ããŠéåžžã«ç°¡åã«æžããŸãããåé ç®ã«ã€ããŠå€ãã®èª¬æãšè°è«ãæžãããšãã§ããŸãã®ã§ãã質åãããã°ã³ã¡ã³ãã§ãçãããŸãã
åç §è³æ
https://github.com/gkopylov/bem
https://github.com/gkopylov/bem_with_rails_and_less_example_app
PS
å·çæç¹ã§ã¯ãbem gemããŒãžã§ã³1.0.0ã䜿çšãããŠããŸããã çŸæç¹ã§ã¯ããã®gemã®ããŒãžã§ã³ã¯1.1.0ã§ã-ãããã¯|èŠçŽ |修食åããããã§ã¹ãã«çŽæ¥æ¥ç¶ããæ©èœãè¿œå ãããŠããŸãã ããã¯ãã¬ã€ã¢ãŠããã¶ã€ããŒãã¬ãã«ãæ¥ç¶ããã®ã§ã¯ãªãããããã§ã¹ãã§çŽæ¥ã¹ã¿ã€ã«ãæ¥ç¶ããé åºãå€æŽã§ããããã«ããããã§ãã
ãã®ãªãã·ã§ã³ã䜿çšããã«ã¯ãçæã³ãã³ãã«-iãã©ã°ãè¿œå ããã ãã§ãã ãããã£ãŠãäœæãããã¹ã¿ã€ã«ã¯ãã¬ãã«ã®ã¹ã¿ã€ã«ãäœæããã«ãããã«ãããã§ã¹ãã«æ¥ç¶ãããŸãã
importããã³requireãã£ã¬ã¯ãã£ãã®äœ¿çšã«ã€ããŠããå°ãæžããããšæããŸããã ã©ã¡ãã®ãã£ã¬ã¯ãã£ãã«ãé·æãšçæããããŸãã ã€ã³ããŒãã®å©ç¹ïŒ
-ããã¯ã¹ã€ã³ãšå€æ°ãæ¥ç¶ã§ããŸã
-ãã¹ãŠã1ã€ã®ãã¡ã€ã«ã«ããŒãžãããçæãå¿ èŠãšããå€ãã®ãã¡ã€ã«ã®ä»£ããã«ããã©ãŠã¶ãŒã«ããéãäžããããŸãïŒãã ããããã¯ãã€ãã¹ã§ããããŸãã以äžãåç §ïŒ
ãã€ãã¹ã®äžã§ïŒ
-CSSã®ãããã°ãå°é£-ããªããã»ããµãšã©ãŒãçºçããå Žåããã®ãšã©ãŒã®å ŽæãèŠã€ããã®ã¯å°é£ã§ã
-ãã¹ããããcssãã¡ã€ã«ãå€æŽããŠãã¹ã¿ã€ã«ã¯æŽæ°ãããŸããïŒã¹ã¿ã€ã«ãæŽæ°ããã«ã¯ãã³ãã³ãrm -rf tmp / asset / *ãå®è¡ããã¬ãŒã«ãåèµ·åããŸãïŒ
requireã®ãã©ã¹ïŒ
-ãããã°ãç°¡åã§ããããããã®å¿ èŠã«å¿ããŠïŒéçºç°å¢ã§ïŒåå¥ã®ãã¡ã€ã«ãã³ã³ãã€ã«ããããã§ãã
-livereloadã€ã³ãžã§ã¯ã·ã§ã³ã®å¯èœæ§ïŒçè«çã«ã¯ãããã¯ã€ã³ããŒããããç°¡åã§ãïŒ
ãã€ãã¹ã®äžã§ïŒ
-ããã¯ã¹ã€ã³ãŸãã¯å€æ°ã䜿çšã§ããªã
-ã³ã³ãã€ã«ãããã¹ã¿ã€ã«ãã¡ã€ã«ã¯ãå€ãã®ãrequireããã£ã¬ã¯ãã£ããããå Žåããã©ãŠã¶ã«é·æéäžããããããšããããŸã
ãã®ããã1ã€ãŸãã¯å¥ã®ãã£ã¬ã¯ãã£ãã䜿çšãã決å®ã¯ããããžã§ã¯ãã«å¿ããŠæ éã«è¡ããªããã°ãªããŸããã
ããã©ã«ãã§scssã䜿çšããããšã«ããçç±ãããå°ãæžããããšæããŸããã äºå®ãsass railã³ãã¥ããã£ã§ã¯ããªããã»ããµã®æ¹ã人æ°ããããããã©ã«ãã§ã¯ãæ°ããã¢ããªã±ãŒã·ã§ã³ãäœæãããšãã«ããã®ããªããã»ããµã䜿çšããgemãæå¹ã«ãªã£ãŠããŸãã ãããŠãïŒ å®éã®ãšãããfaost㯠æ£ããèªèãããŠããã http: //lesscss.org/features/#parent-selectors-featureã«ã¯ããŸããããŸã ã ïŒBEMã«ã¡ããã©é©ããåªããæ©èœããããŸã-ããã¯ã¢ã³ããµã³ãã®äœ¿çšã§ãã ãã§ã«ãBEMã³ãã¥ããã£ã®å€ãã¯ãã¢ãã£ãã¡ã€ã€çšã®å¥ã®ãã¡ã€ã«ïŒå°ããå ŽåïŒãäœæããããšããé ãããããããã¯ã䜿çšããŠãã¡ã€ã«ã«ã¢ãã£ãã¡ã€ã€ãçŽæ¥æžã蟌ã¿ãŸãããã®ãµã¹ã¢ã³ããµã³ãã䜿çšãããšããããã¯ã®ã¹ã¿ã€ã«ã§ãã®ãããªããšãçŽæ¥å®è¡ã§ããŸãã