14

Is it possible to declare new variables within the jQuery template syntax? I'm hoping to achieve the equivalent of this (which does not work):

{{var test = "test"}}

<div>
    ${test}
</div>
2
  • Why wouldn't you declare the variable where you use the template? or make your own custom "template extension" that takes common practices and separates them from typical template usage. Feb 28, 2011 at 21:28
  • 2
    Because the context of the variable is related to the template logic and should remain encapsulated within the template. For example, what if I want to set a boolean flag if something is encountered in an {{each}} loop, and then have the template display something different in a few places later on, depending on that boolean value.
    – Wilco
    Feb 28, 2011 at 21:31

2 Answers 2

23

I know this has already been answered and accepted, but here is another solution:

${( $data.localVariable = 'SOMETHING' ),''}

A jsFiddle example: http://jsfiddle.net/brettwp/PrfZ5/

5
  • This is a really clean implementation that I've started using. With this syntax, it's very easy to define local variables in my templates that would otherwise require expensive calls into my model. (I'm currently using this with KnockoutJS.) Thanks for a straightforward solution that doesn't rely on tricks or hacks. Apr 20, 2012 at 20:26
  • simple and elegant. definitely the way I'll do it from now
    – MatteoSp
    Jan 8, 2013 at 10:40
  • Thanks. It really works. I use it when I have a lot of if, else, if, else, etc. Jan 14, 2013 at 10:46
  • I'm into a similar situation, and I need to assign the value to the localVariable at runtime. I tried using the object notations, but didn't work. Is it possible? I want to do something like this: ${( $data.localVariable = testObject.name ),''} testObject is the response from the server which I'm operating on in my template. TIA. Nov 7, 2014 at 6:49
  • If you pass ${localVariable } to another template, how do you then access that data in the next template? Dec 8, 2015 at 3:47
9

This is pretty lame, but one trick that might work is:

  {{each(i, test) ["test"]}}
    blah blah ${test} blah
  {{/each}}

I'm not 100% sure however about using an array constant expression like that; I'll set up a fiddle. (edit yes it works :-)

4
  • Woa, cool. What exactly is ["test"]? A quick search for array constant expression didn't appear to be helpful. Feb 28, 2011 at 23:17
  • It's just an array with a single string in it ("test"). The idea of "{{each}}" is to iterate, but a side-effect is that you get to set a variable to the value of an expression. With that hack, you could put any expression you wanted in place of ["test"] for the value of the loop variable - it just has to be an array, with one value in it :-)
    – Pointy
    Feb 28, 2011 at 23:21
  • Haha, definitely one heck of a hack! Accepting answer because it works, upvoting for the creativity. :-D
    – Wilco
    Mar 1, 2011 at 4:20
  • Yup, I think this is a crafty solution, sir.
    – hayesgm
    Oct 25, 2011 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.