1

Take this code for example:

title: 'Hello',

The variable is called title and the value is hello. I would be able to call it via

{{ title }}

and get the result.However, recently I needed to create a variable variable. I do not want the name of the variable to be static yet dynamic.

var nameIWant = "blah";
...
nameIWant: 'Dynamics!',
...

If I was to call {{ blah }} it would not work, however, calling {{ nameIWant }} does

How do I fix this? Is it possible?

Max
  • 2,710
  • 1
  • 23
  • 34
  • The limitation with variable keys in Object literals, like `nameIWant: 'Dynamics!'`, is described in a number of SO Q&As, e.g. [How do I add a property to a Javascript Object using a variable as the name?](http://stackoverflow.com/questions/695050/how-do-i-add-a-property-to-a-javascript-object-using-a-variable-as-the-name) and [Is this Javascript object literal key restriction strictly due to parsing?](http://stackoverflow.com/questions/2873163/is-this-javascript-object-literal-key-restriction-strictly-due-to-parsing) – Jonathan Lonowski Aug 25 '14 at 17:43
  • That seems... wicked. The point of templates is precisely that they do not change, but the content does. I'm sure that you have a good reason for your case, but my guess is that making it work would probably require writing a template for the template (a _metatemplate_ ?) and doing two renderings to get the final result. Wicked, indeed. – jdehesa Aug 25 '14 at 17:44
  • https://github.com/twitter/hogan.js/issues/101 I found this :( – Max Aug 25 '14 at 20:00

1 Answers1

0

You can't de-reference variables like this. Moustache is intentionally very simple and this is anything but.

You'll have to do the remapping at the source level, as the template engine can't do it for you.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Seems like a lot of work. But, I am just wondering, what is compiled first, moustache or javascript? Maybe If I was to change the compilation order it might work – Max Aug 25 '14 at 17:48
  • You'll have to fork Moustache and implement this yourself because it's not a core feature. – tadman Aug 25 '14 at 18:10