Tibor SzászInteraction matters

  • Home
  • About me
  • Projects
  • Reqest price offer
  • Contact me
  • RSS

2010 0202

JavaScript template engine upgraded

JavaScript template engine upgraded I'm a big fan of separating the business logic and the presentation layer. This can be easily done by PHP, ROR, Java, etc. but when you try to do it in JavaScript it's not obvious how to do it, or which is the best way.

I've researched a lot on the topic and found two schools that is used by JS template libraries:
- Template language parsing
- Procedural approach
Both are great ways to generate, it only depends on your needs.

Here are some projects worth noting:


  • JST - JavaScipt templates - Big and evolved library, Smarty like syntax, GPL
  • SWATO - Very similar to the above, it's created for Java develpers. Example.
  • Chain.js - a nice tutorial about this.
  • PURE - this is a hybrid one and library free
  • jTemplates - a jQuery plugin
  • JTL - A Django template engine port
I played with the last one. It had some basic functions such as for loop, counter, reverse order, if syntax for checking if a value exists. The only problem was that it didn't had a comparison operator for strings and variables, so I decided to upgrade it.


Using it is easy:

var tpl = new Jtl( document.getElementById('tpl').value );

var data = {
  namelist :[
	{name : 'Johnnn' , job : 'Director',topicID:[1,2,3,4]},
	{name : 'Nick' , job : 'Manager',topicID:[4,3,6,5],test : 'Anything'},
	{name : 'Tony', topicID:[5,66,7]}
  ]
};
document.getElementById('destination').innerHTML = tpl.render( data );

And the template to be fetched:
{%for people in namelist%}
	
  • {{forloop.counter}}.{{people.name}} {%if people.job eq 'Manager'%} Yes, I'm the {{people.job}}...{%endif%}
      {%for tid in people.topicID%}
    • {{tid}}
    • {%endfor%}
  • {%endfor%}

    attachmentFile attached to post: javascript_template_engine.zip


    Comments


    Add new comment


    Your name
    Email
    Your avatar
    Website
    Comment

    Briefing

    Me myself and I
    I love coding, especially for the web. I'm in the online industry for 6 years. My favourite toys are:
    JavaScript, PHP, XHTML, MySQL, Canvas, Flash
    Váltás magyar nyelvre

    Topics

    • Coffee break
    • Design & stuff
    • Generative arts
    • Programming

    Friends

    • Kárpáti Róbert
    • Royaljerry
    • Bagi Zoltán
    • Pegazoo
    • Charles Alexander Deli
    • Bence Bangócs
    • Lunakid
    • Rapp design

    Feeling so social

    • Facebook
    • IWIW
    • TurulMeme

    Tags

    javascript (12) php (6) mootools (4) iscaffold (3) codeigniter (3) crud (3) tutorial (2) google (2) renders (2) structure-synth (2) css (2) template (2) free (2) google-buzz (1) script (1) forge (1) hack (1) table (1) open-source (1) rc1 (1) infovis (1) rss (1) interface (1) canvas (1) design, (1) user (1) experience (1) soundcloud (1) resizing (1) joomla (1) bermuda-triangle (1) wordpress (1) drupal (1) data-visualisation (1) data-uri (1) konami-codes (1) tagging (1) slimbox (1) bugfix (1) cfdg (1) blackout (1) firefox-extension (1) utf8 (1) class (1) slideshow (1) nl2br (1) experiment (1) svg (1) library (1) ajax (1) sitebuild (1) tabs (1) inspiration (1) lists (1) drag-and-drop (1) wikipedia (1) dns (1) trick (1)




    All rights reserved, ©2008-2010 - Built on CodeIgniter framework - Konami codes - Mostly Valid XHTML 1.1 - Valid CSS 2.1