blam.js
Simple, Tiny, Fast (enough)
Example usage (in CoffeeScript):
blam-> html head title "Hello" script src:"/my-script.js" body article id:"main" sectioncontent p"Loading..." asidesidebar "Nav here"
You can use it in JavaScript too (but it's much prettier in CoffeeScript):
;
If you like, you can compile string templates as well (you'll need to use the javascript syntax):
Notes
Clocks in at ~1K minified and gzipped.
MIT Licensed.
Unit tests can be found at: darthapo.github.com/blam.js/test
Please let me know if any of the tests break in your browser!
If the first argument to a tag block is an object literal then its key/value pairs are used as tag attributes.
blam -> div id:"posts"'data-role':"post" p"content here..." # => '<div id="posts" data-role="post"><p>content here...</p></div>'
Blam will pass initial arguments on to the markup block:
blam name:'Matt' div id:'user-block' p"Hello "username
Markup blocks can be "compiled" into an executable function (good for often repeated bits of code):
template= blamcompile div id:'user-block' p"Hello "username html+= templateuser for user in all_users
For array elements you can use the each
tag:
template= blamcompile div id:'user-block' eachusers p"Hello "username html+= templateall_users
You can create partials/custom tags:
blamdefine 'layout'-> articlecontainer header h1 "My App" sectionbody __arguments footer p"© me!"
You can then use it like a tag:
blam @posts layout divposts eachposts diventry postbody
You can turn off the fancy auto css class support:
blamfancyno # You have to use the attributes hash to add classnames now: blam -> div class:'my-class'"Content"
Scopes
You can add a single object to a markup block's scope when pre-compiling templates:
data= name: 'He' age: 'Old Enough' template= blamscopedatacompile -> div name" is "age html= template#=> "<div>He is Old Enough</div>"
Backbone Mini Example
@template: blamcompile divtweet imgavatar src: tweetprofile_image_url alt: tweetfrom_user_name divstatus tweetstatus divfooter "@" tweetfrom_user " said on " tweetcreated_at : -> @$elhtml TweetViewtemplate @modeltoJSON
Node.js
Blam is available as an npm package as well. To install:
npm install blam
Or, use the -g
option to install it globally:
npm install blam -g
Then require
it as per usual in node:
var blam= blam; blam
Todo
-
Need to cleanup this ReadMe and create some proper docs.
-
Would like to support all the modern browsers and possibly as far back as IE8?
-
Need to test AMD support.
-
Extract and expose tag building helpers.
eval
and with
Evil?
Aren't No! They're just misunderstood. Blam.js is a good example of why they are useful as language features. They allow excellent DSL generation.
That having been said, it is a good rule of thumb that if you don't know when is best to use them; don't.