Velocity2JS: Templating within

Written by on May 15th, 2007 in Ajax News.

Velocity is a popular templating system for Java developers. Velocity2JS takes the engine and has it output JavaScript functions.

Example Velocity Template

HTML:

  1.  
  2. Hello $world.name!
  3. #foreach( $greeting in $greetings )
  4.    <tr>
  5.         <td>$greeting.language</td>
  6.         <td>$greeting.text</td>
  7.    </tr>
  8. #end
  9. </table>
  10. </body>
  11. </html>
  12.  

JS Output

JAVASCRIPT:

  1.  
  2. function helloworld(context) {
  3.         var text = new StringCat();
  4.         text.push(’<html><body>Hello ‘);
  5.         text.push(context.world.name);
  6.         text.push(’<table>’);
  7.         for (var i1=0;  i1<context .greetings.length; i1++) {
  8.                 var greeting = context.greetings[i1]);
  9.                 text.push(’   <tr>      <td>’);
  10.                 text.push(greeting.language);
  11.                 text.push(’</td><td>’);
  12.                 text.push(greeting.text);
  13.                 text.push(’</td>        <td>’);  
  14.         }
  15.         text.push(’</td></context></table></body></html>’);
  16.         return text.toString();
  17. }
  18.  

Source: Ajaxian
Original Article: http://ajaxian.com/archives/velocity2js-templating-within

Leave a Reply

You must be logged in to post a comment.



Site Navigation