⬆️ ⬇️

BEM with a human face and backend integration

The layout of modern web-projects is difficult, time consuming and expensive. It would seem that with the transition of IE to automatic updates, HTML5, the end of support for Win XP, we all need to heal in a fairyland with a pony and a rainbow. Why did not it become easier?







All this makes you think about optimizing work with the frontend.



I want to:





')

The simplest solution is to abandon server templating at all, switch to REST-API and SPA



pros





Minuses







The cons are pretty serious. What are the options if the templates are on the server?




Frontedschik edits server templates



pros





Minuses







Front-end user edits his templates in pure html without server inserts (possibly with preprocessors) and sends to the backend



pros





Minuses







Front-end user uses server engine during layout and independently allocates layouts, blocks and patterns.



pros





Minuses







I tried all the approaches described. My experience says that, depending on the type of project, other things being equal, the first and last options work best.


And here bam?



Perhaps BEM is one of the most holivore themes of our time, so I can't help but insert the picture below.



.



I tried to work with “BEM” and “semantic” layout and came to the conclusion that constant changes in requirements and modifications lead to the frequent need to change the markup and be tied to tag names and cascades in CSS - the idea is so-so.



What are bad cascades?

The idea of ​​"throwing out" from the "cascading" style pages cascade at first glance seems seditious, but:





My checklist



General requirements

  1. Layout should be cross-browser and on the grid, correction of errors in indents in the design is allowed
  2. Layout must be implemented by BEM-methodology
  3. Layout must undergo w3c validation
  4. Layout to meet the requirements of this checklist
  5. Tables should only be used to present tabular data, nested tables are prohibited.


Forms

  1. Forms must always be designed using the form tag, the tag must contain an action attribute
  2. Buttons must be tagged as input or button.
  3. For phones, you must use the input mask
  4. Validation must be implemented


BEM

  1. It is forbidden to use element id and tag names, except for the exceptions below, to set styles
  2. Classes should be named as follows: some-block__ some-element_ some-modificator
  3. It is forbidden to use inline-styles.
  4. The names of the blocks and elements should correspond to their purpose and function, and not to how they are displayed.

    If the blocks perform different functions, but look the same, they should be arranged as one block with a neutral name, reflecting their similarity. No unnecessary duplication of code
  5. It is necessary to use normalization for all tags that cannot have a class name (the cases are described below), the use of reset.css is not allowed. Using normalize.css is permissible, exactly like the normalization variant only for tags for which it is impossible to set a class
  6. It is forbidden to use cascading styles with a depth of more than one element (for example, table td ), except for the following situation: depending on the block modifier applied, the child elements / blocks should be displayed differently. The cascade will be short, with dynamic switching of the java-script modifier, the implementation is much simpler and more efficient from the point of view of interaction with the DOM than in the case of modifying the styles of all child elements.
  7. B-prefixes for blocks are not used
  8. Global modifiers must be declared as a separate class, for example _uppercase . Underscore at the beginning indicates that it is a modifier, not a block. For adaptive layout, it is necessary to use the _desktop , _tablet and _phone global modifiers for the blocks that need to be displayed only on a specific device. It is preferable to use the mobile-first approach: from simple to complex
  9. Styles should be applied to tags through the parent cascade in cases where it is impossible to control classes of elements, for example:

    • The content is entered by the user through a form on the website, is created dynamically or comes from other sources beyond our control.
    • Controls in the form: it is preferable to use the parent cascade, because the elements of the form can be created using the server code. Additional reformatting is required to preserve the styles of the elements. This is undesirable.


Formatting and file system

  1. Using one large CSS file or JS file is not allowed, you must use an extensive file structure, namely:
  2. Each block is located in its own folder, reusable blocks and main layouts are located in the shared folder.
  3. The description of the block and all elements is located in one file called <BLOCK_NAME> .css . If the description of blocks and elements exceeds 600 lines, it is necessary to decompose it.
  4. Class names and CSS rules are sorted alphabetically.
  5. The modifier should override no more than 40% of styles, otherwise it is better to create a new block.
  6. The block HTML template is in the same folder (server or client templating technology is used)
  7. Layouts are separated from pages, all necessary pages are “cut” by separate files


Bundling, minification and build

  1. All resources must be prepared for production and divided into bundles. Bundles should be grouped in such a way that their number is minimal, and the bundle weight does not exceed 250KB.
  2. The use of server technology for splitting html layouts into layouts, pages and blocks is allowed and encouraged.
  3. Delivery acceptance is made after the "assembly". The result is n html pages and an index.html file with links to all the folded pages. It is permissible to transfer the layout in the “source code”, with the attached bat / makefile and the “collector”, if the size of the “collector” does not exceed 10MB and can be guaranteed to be run on the OS of the receiving layout.
  4. Links in css and js files must be absolute, otherwise there is a risk that they will not work correctly after minification





A selection of good materials on BEM:





Just awesome getting started guide for new employees.

It is difficult to use as a checklist because of the scale of this document: habrahabr.ru/post/114256 . There is an abbreviated version, which is suitable as a checklist: github.com/delka/html5checklist .



BEM check

 s=document.createElement('script');s.src ="//2gis.imtqy.com/makeup/autoload/script.js";document.body.appendChild(s) 

Source: https://habr.com/ru/post/246143/



All Articles