Javascript Templates: TrimPath
I’m working on a little proof of concept project and started the task of writing a simple client-side template parser. After a few hours and some significant progress, I ran into a snag where the solution I had working just wasn’t as elegant as I wanted it. “What am I doing??” I asked myself. Ten seconds and one google search later I found Trimpath’s Javascript Templating library.
With this little library you define a template (inside a hidden element on the page or in a javascript string variable) and then call the parser, passing the template string/element and an associative array. As we start creating components that pull data via ajax and then display it, this kind of cilent-side template parsing is going to be more and more useful.
Examples:
{if customer != null && customer.balance > 1000}
We love you!
{/if}
{if user.karma > 100}
Welcome to the Black Sun.
{elseif user.isHero}
Sir, yes sir! Welcome!
{if user.lastName == "Yen"}
Fancy some apple pie, sir?
{/if}
{/if}
{for varName in listExpr}
...
{/for}
{for varName in listExpr}
...main body of the loop...
{forelse}
...body when listExpr is null or listExpr.length is 0...
{/for}
Leave a Reply
You must be logged in to post a comment.