views:

103

answers:

1

Is it possible to create if sentences inside a jQuery tmpl template?

<script id="template" type="text/html">
    <h1>${someVar}</h1>
    if (${intro}!="")
        <small>${intro}</small>
    endif
    <p>${restOfVariables}</p>
</script>

Now, this would only write out the if as text, so is there any way to do something like this? Or would I have to create two different templates and do the check in my js before calling the template?

+2  A: 

According to these docs, you can do:

<script id="template" type="text/html">
    <h1>${someVar}</h1>
    {{ if intro != "" }}
        <small>${intro}</small>
    {{ /if }}
    <p>${restOfVariables}</p>
</script>
sje397
Aagh you had a 14 second lead! OK fine I'll delete my basically-identical answer...
Domenic
Nice! But Resig's `tmpl` file crashed on me with if/else statements. Found this fork on github that fixed the issue, though: http://github.com/jchadwick/jquery-tmpl
peirix