On Thu, 21 Nov 2013 18:19:18 +0100, Sönke Ludwig wrote:

Am 21.11.2013 16:51, schrieb Manu Evans:

On Thu, 21 Nov 2013 15:26:16 GMT, Manu Evans wrote:

script(type="text/javascript")
  |var x = 0
  - foreach(x; things)
    | #{x},   // <-- it can't find x
  |];

Problem:
Error: undefined identifier x, did you mean template to(T)?

It seems that the scope of the '|' block under the foreach isn't scoped correctly, x is not local...?

You know, I reckon there's a possible library function here. What I need to do is make some D data available to the javascript.
It would be awesome to have a function which would produce a javascript declaration from a D value/structure.

Actually, as far as I can tell, all the '|' beneath script are ignored.
So just the same as :javascript, everything beneath script() seems to be copied verbatim as well.

Damn, you are right, script and style tags get special treatment. So the
correct way is this and control statements are not really possible:

script
	var x = [#{join(things, ",")}];

The idea of automatically mirroring D data as JavaScript is nice. A good
approximation should be to use JSON:

script
	var x = !{serializeToJson(things)}


Yup, that's what I ended up with, does my job, but it could be better.
Some things not satisfied by that approach:

  • Writing enum definitions (ie, the key:value pairs) isn't really serialisation, but still useful to mirror in js.
  • SysTime produces a string, it would be better to produce a js Date object which can be operated on.
  • I want to be more selective about the members that are presented to the webpage. Ie, it's a different set of data when serialising to disk for storage, or serialising to web for presentation. @optional @ignore only work for one case.