tmpvar / weld

Template antimatter for Node.js (Browsers too!)
673 stars 39 forks source link

How to do conditions? #7

Closed xdamman closed 13 years ago

xdamman commented 13 years ago

This projects looks very interesting. I've always found templating systems to be bad hacks.

The question is how do you do conditions (if else)? Eg. (in ejs) <% if(person.description) { %>

<%=person.description%>

<% } %>

Thanks!

heapwolf commented 13 years ago

All of the conditional logic goes into your javascript file, with weld you can use map functions. This is a contrived example obviously, but it demonstrates how...

  window.weld($('.people')[0], {
    person: [
      {
        name: 'John',
        job: [
          'guru', 'monkey', 'tester'
        ]
      },
      {
        name: 'Bob',
        job: [
          'supervise', 'yell'
        ]
      }
    ]
  },
  {
    map: function(el, key, value) {
      if(key==='name' && value==='John') {
         return $(el).addClass('edgeCase');
      }
    }
  });