i-bem.js
and using the BEMHTML
template BEMHTML
. Helping to do all this will be bem tools
, in particular - a tool for developing bem server
.0.5.21
was relevant.5ac5d2d2567ca6d52d82f95b219bca6f49ef5cc4
. $ git clone git://github.com/bem/project-stub.git my-pretty-project $ cd my-pretty-project/ $ git reset --hard 5ac5d2d2567ca6d52d82f95b219bca6f49ef5cc4 $ rm -rf .git $ git init
make
info: Server is listening on port 8080. Point your browser to http://localhost:8080/
bem server
is launched on your computer - a development tool that will automatically rebuild your project if you make changes to it.desktop.blocks
directory, and the pages in the desktop.bundles
directory.desktop.bundles
stores a “set” of blocks. These can be frequently used blocks of several pages (what is commonly called common
), sets that combine several pages ( all
, if all pages are combined) or - the simplest case - sets of blocks, each of which corresponds to one page. Here will be considered the last, simple option.desktop.bundles/index/index.bemjson.js
.head
block: { block: 'head' }
<div>
appeared in it. <!DOCTYPE html> <html class="i-ua_js_yes i-ua_css_standard"> <head>...</head> <body class="b-page b-page__body"> <div class="head"></div> </body> </html>
head
block we place a layout
block with two elements: left
and right
. content: [ { block: 'head', content: { block: 'layout', content: [ { elem: 'left', content: 'left here' }, { elem: 'right', content: 'right here' } ] } } ]
<!DOCTYPE html> <html class="i-ua_js_yes i-ua_css_standard"> <head>...</head> <body class="b-page b-page__body"> <div class="head"> <div class="layout"> <div class="layout__left">left here</div> <div class="layout__right">right here</div> </div> </div> </body> </html>
layout
block in CSS technology.bem create
command. $ bem create -l desktop.blocks/ -T css -b layout
desktop.blocks/layout/layout.css
, in which there is already a selector corresponding to the block file. The rule must be supplemented according to the purpose of the block.layout
search form and logo blocks do not need to be implemented independently. They are already implemented in the library bem-bl , simply declare them on the page. That is, insert the BEMJSON description of the block into the desktop.bundles/index/index.bemjson.js
b-logo
block we use provides only the necessary markup. CSS for it, each developer can write himself, because everyone needs a different markup.b-logo
block at our override level. $ bem create -l desktop.blocks/ -T css -b b-logo
b-search
block: $ bem create -l desktop.blocks/ -T css -b b-search
b-page
block templates, creating the same block at our level. BEMHTML is used as a template engine. $ bem create -l desktop.blocks/ -b b-page -T bemhtml
desktop.blocks/b-page/b-page.bemhtml
you need to write code that wraps the content of the block into an additional container. block b-page, content: { elem: 'body-i', content: this.ctx.content }
<!DOCTYPE html> <html class="i-ua_js_yes i-ua_css_standard"> <head>...</head> <body class="b-page b-page__body"> <div class="b-page__body-i"> <div class="head"> <div class="layout">...</div> </div> </div> </body> </html>
$ bem create -l desktop.blocks/ -T css -b b-page
desktop.blocks/b-page/b-page.css
can be copied from here: https://gist.github.com/4175763 $ bem create -l desktop.blocks/ -T css -b head
desktop.blocks/head/head.css
: https://gist.github.com/4175776 .goods
block, and the declaration contains the necessary data. { block: 'goods', goods: [ { title: 'Apple iPhone 4S 32Gb', image: '1450827127820366493466', price: '259', url: '/' }, { title: 'Samsung Galaxy Ace S5830', image: 'http://mdata.yandex.net/i?path=b0206005907_img_id5777488190397681906.jpg', price: '73', url: '/' }, ... }
$ bem create -l desktop.blocks -b goods
desktop.blocks/goods/goods.bemhtml
you need to write code that turns JSON with data into block elements. And also, using the tag
mode, specify which DOM elements to represent the block and its elements. block goods { tag: 'ul' ... elem item, tag: 'li' elem title, tag: 'h3' }
<!DOCTYPE html> <html class="i-ua_js_yes i-ua_css_standard"> <head>...</head> <body class="b-page b-page__body"> <div class="b-page__body-i"> <div class="head">...</div> <ul class="goods"> <li class="goods__item"> <h3 class="goods__title">Apple iPhone 4S 32Gb</h3> <img class="goods__image" src="1450827127820366493466"/> <span class="goods__price">259</span> </li> <li class="goods__item">...</li> <li class="goods__item">...</li> </ul> </div> </body> </html>
b-link
block from the bem-bl
library. { elem: 'price', content: { block: 'b-link', url: item.url, content: item.price } }
goods
block. { block: 'b-link', mix: [{ block: 'goods', elem: 'link' }], url: item.url, content: item.price }
... <ul class="goods"> <li class="goods__item"> <h3 class="goods__title">Apple iPhone 4S 32Gb</h3> <img class="goods__image" src="1450827127820366493466"/> <span class="goods__price"> <a class="b-link goods__link" href="/">259</a> </span> </li> <li class="goods__item">...</li> <li class="goods__item">...</li> </ul>
$ bem create block -l desktop.blocks/ -T ie.css goods
desktop.blocks/goods/goods.ie.css
can be taken at Gist https://gist.github.com/4177174deps.js
technology. $ bem create -l desktop.blocks/ -T deps.js -b goods
shouldDeps
by specifying that a b-link
block is needed. ({ shouldDeps: [ { block: 'b-link' } ] })
box
that does what I need../bem/make.js
, by analogy with neighboring libraries. getLibraries: function() { return { 'bem-bl': { type: 'git', url: 'git://github.com/bem/bem-bl.git', treeish: '0.3' }, 'bemhtml' : { type: 'git', url: 'git://github.com/bem/bemhtml.git' }, 'john-lib' : { type: 'git', url: 'git://github.com/john-johnson/j.git' } }; }
desktop.bundles/.bem/level.js
. exports.getConfig = function() { return BEM.util.extend(this.__base() || {}, { bundleBuildLevels: this.resolvePaths([ '../../bem-bl/blocks-common', '../../bem-bl/blocks-desktop', '../../bemhtml/common.blocks', '../../john-lib/blocks/', '../../desktop.blocks' ]) }); };
bem server
. The current process will have to be interrupted and typed make
again.box
. I can just wrap my blocks with them. But in order to save markup, you can mix 2 blocks on one DOM node. This is called a mix
.head
block with the box
block by changing the page code. { block: 'head', mix: [ { block: 'box' } ], content: ... }
<!DOCTYPE html> <html class="i-ua_js_yes i-ua_css_standard"> <head>...</head> <body class="b-page b-page__body"> <div class="b-page__body-i"> <div class="head box"> <div class="layout">...</div> </div> <ul class="goods">...</ul> </div> </body> </html>
box
block depending on the head
block. $ bem create -l desktop.blocks/ -T deps.js -b head ({ shouldDeps: [ { block: 'box' } ] })
goods
block template, we mix each item element with the box
block. content.push({ elem: 'item', mods: mods, mix: [{ block: 'box' }], content: ...
<!DOCTYPE html> <html class="i-ua_js_yes i-ua_css_standard"> <head>...</head> <body class="b-page b-page__body"> <div class="b-page__body-i"> <div class="head box"> <div class="layout">...</div> </div> <ul class="goods"> <li class="goods__item box">...</li> <li class="goods__item box">...</li> <li class="goods__item box">...</li> <li class="goods__item goods__item_new_yes box">...</li> <li class="goods__item box">...</li> <li class="goods__sizer">...</li> ... </ul> </div> </body> </html>
box
, which appeared on my project due to the connected third-party library, also provides dynamic JavaScript functionality — it can collapse.head
block, indicating that the admixed box
has a JavaScript implementation: mix: [{ block: 'box', js: true }]
switcher
element inside the block. content: [ { block: 'layout', ... }, { block: 'box', elem: 'switcher' } ]
box
not enough for me. I want it to fold not only vertically, but horizontally. At the same time, I cannot make changes to another library. But due to the fact that the JavaScript block is written using a declarative framework from the i-bem block , I have the opportunity to change (redefine or define) the behavior of the block at my level. bem create -l desktop.blocks -T js -b box
desktop.blocks/box/box.js
you need to leave only the onSetMod
section, which describes the response to the installation of modifiers. onSetMod : { }
closed
: onSetMod : { 'closed': { 'yes': function() { // some functionality here }, '': function() { // some functionality here } } }
bem create
command: bem create -l desktop.bundles -b contact
-T
flag can be omitted, because bem create
knows that the blocks created at this level must be represented in the BEMJSON technology due to the settings of the desktop.bundles
level. So, the file desktop.bundles/contact/contact.bemjson.js
appears with minimal content for the page.bem server
will collect its HTML, JS and CSS files at the time of the first appeal.bem server
working and reassembled those parts of the project that need to be changed when the pages are updated.bem make
. ./node_modules/bem/bin/bem make
Source: https://habr.com/ru/post/162385/
All Articles