Evil Blocks
Evil Block is a tiny framework for web pages. It splits your application to separated blocks with isolated styles and scripts.
Sponsored by Evil Martians. Role aliases taken from role and role-rails by Sasha Koss.
Quick Example
Slim Template
.user-page
.title User Name
.gallery-control.is-big
// Evil Blocks add @data-role alias to Slim
// .class to bind styles, @data-role to bind JavaScript
a.gallery-left@next-photo href="#"
a.gallery-right@prev-photo href="#"
img src="photos/1.jpg"
img src="photos/2.jpg"
img src="photos/3.jpg"
Sass Styles
// Block
.user-page
// All elements must be inside blocks (like in namespaces)
.title
font-size: 20px
// Block
.gallery-control
position: relative
img, .gallery-left, .gallery-right
position: absolute
// Modificator
&.is-big
width: 600px
CoffeeScript
# Will execute callback only if .gallery-control is in current page
evil.block '.gallery-control', ($, b, block) ->
current = 0
showPhoto = (num) ->
# b-function finds only inside .gallery-control
b('img').hide()
b("img:eql(#{ num })").show()
initState: ->
showPhoto(current)
buttons: ->
# Shortcuts for data-role="next-photo"
b.nextPhoto.click ->
showPhoto(current += 1)
slideShow: ->
# You can communicate between blocks by simple events
block.on 'slideshow-start', ->
setTimeout( -> b.nextPhoto.click() , 5000)
# Will execute callback only on user page, where .user-page exists
evil.block '.user-page', ($, b, block) ->
startSlidehow: ->
b('.gallery-control').trigger('slideshow-start')
Styles
- You split all you tags to “blocks” and “elements”. Elements must belong to block. Blocks can be nested.
- Blocks classes must have special suffix, that can’t be accidentally used
in elements. For example.
-page,-layout,-controlsuffixes (for example,.user-page,.header-layout,.gallery-control). - All styles must have block class in condition. For example,
.user-page .title,.user-page .edit. So all you styles is protected from accidentally same classes (it’s important, if you join all your CSS files in one file). - If block can be nested in another blocks (like common controls), adds prefix
to all its elements (like
.gallery-left). If block is a root block (like.user-pageor.header-layout) be free to use short classes. - Classes to modificate elements or blocks, must be like sentence without a noun
(for example,
.is-big,.with-header).
JavaScript
- Unobtrusive JavaScript.
- Write animation and states in CSS. JavaScript just changes CSS classes.
- Avoid rendering. Send from server HTML, not JSON.
- Wrap scrips to revive blocks in
evil.block(selector, callback). It will executecallbackonly if blockselectorexists in current page. So you can be free to join all JS files in one. evil.blockwill send tocallbackthree arguments:$is jQuery,bis “b-function”,blockis blocks found byselector.- B-function is like jQuery function, but find only inside finded block
(alias
b('a') = $('a', selector)). - Bind JavaScript to
data-roleattribute to be free to change styles and classes without dangeros of breaking scripts. - You can easy find special
data-roleby b-function. It will has properties for all roles inside block (@role-namewill be camelized tob.roleName). - If
callbackreturn object,evil-blockwill execute all it methods. It’s useful to split your script to several initializer with separated variables scope.
Install
Ruby on Rails
Add evil-block-rails gem to Gemfile:
gem "evil-blocks-rails"
Load evil-blocks.js in your script:
//= require evil-blocks
Ruby
If you use Sinatra or other non-Rails framework you can add Evil Blocks path to Sprockets environment:
EvilBlocks.install(sprockets)
And change Slim options to support @data-rule shortcut:
EvilBlocks.install_to_slim!
Then just load evil-blocks.js in your script:
//= require evil-blocks
Others
Add file lib/evil-blocks.js to your project.
See Also
- Evil Front – pack of helpers and libraries for Ruby on Rails and Evil Blocks.