STAN - Shortest Template Engine
For 30 lines of source code (no dependencies) you will get:
- First class support of JavaScript inside of templates
- Ability to call any JS function or JS librery
- DOM access while rendering template ( so you can add some crazyness to your templates )
- Data escaping ( can be turned off )
- Template grammar validation
- Tracing ( throu console.log method )
- Debugging ( throu debugger statement )
- Template Partials ( call one template from another and pass any arguments )
- Template Compilation ( for good speed )
- JSON output ( or any other format ) is possible via 'raw' method
- Unlike other 'short' template engines - this one can be used in real project :)
How to use (Browser)
Install via bower
bower install STAN
Use it
Hello STAN <!-- add STAN refrence -->
How to use (Node.js)
Install via npm
npm install stan
Use it
var STAN = ; //prepare some datavar data = somevalue: "Hello STAN"; //write template { divcontextsomevaluediv}//compilevar compiledTemplate = STAN;//executevar result = STAN;
More advanced example
//noprotect //this line disable jsbin jsfiddle protection //this is datavar data = keys:3023null key:"<b>test</b>"; //this is template{ for var i=0; i < contextkeyslength; i++ divb; if contextkeysi === 0 spanspan; else ; spanspan; dive; with context divkeydiv; } //this is also a template but it will be called as a sub template{ span'class="red"'context!==null?context:'null value'span;} documentbodyinnerHTML = STAN;
CoffeeScript
data = "a""b""c" _template -> for item in context divb partial _itemitem dive _item -> divcontextdiv hr STANrun STANcompile _templatedata
But what about speed?
STAN have power of fullfledge template engine and speed of 'micro template' frameworks (but you have to get compiled template and save it into the file, it will be better in the future)
http://jsperf.com/stan-speed-test
BTW
This template engine is called STAN in a honor of Stan Marsh - "a normal, average, American, mixed-up kid", from South Park :)
( image and a character itself are belong to South Park authors, I don't own it )
And a main question.. why?
Science isn't about WHY. It's about WHY NOT. Why is so much of our science dangerous? Why not marry safe science if you love it so much. In fact, why not invent a special safety door that won't hit you on the butt on the way out, because you are fired.
(c) Cave Johnson - Portal 2