Search code examples
jqueryjquery-templates

jquery tmpl with if else


I'm trying to cache my markup into a $.template. Inside of that, I'd like to add a conditional where, if a variable is empty, the output is reflected with this.

Here is a snippet of my attempt. When I run this, the event type is either correct, or blank. "(No Moderators Listed)" never shows up.

What am I doing wrong?

var markup = '<div class="new_line general_heading">' +
                 '{{if EventType != ""}}' +
                     '${$EventType}' +
                 '{{else}}' +
                     '(No Moderators Listed)' +
                 '{{/if}}' +
             '</div>' +
             '<div class="gray_rule allclear"></div>';

Solution

  • There's a small error in your template that could be causing issues:

    var markup = '<div class="new_line general_heading">' +
                     '{{if EventType != ""}}' +
                         '${EventType}' +
                     '{{else}}' +
                         '(No Moderators Listed)' +
                     '{{/if}}' +
                 '</div>' +
                 '<div class="gray_rule allclear"></div>'
    

    Changed ${$EventType} to ${EventType}. After that change, the template seems to work fine.

    Example: http://jsfiddle.net/tx97s/