RejectedSoftware Forums

Sign up

:javascript / :css blocks

It seems I can't use '-' blocks beneath ':' blocks, for instance:

:javascript
  var x = 0;
  - #{ "var y = " ~ d_variable ~ ";" }
  var arr = [
  - foreach(x; things)
    #{ x ~ "," }
  ];


You get the idea... these don't seem to be getting executed, they're just pasted verbatim into the javascript.
Is there a better approach?

Re: :javascript / :css blocks

Am 21.11.2013 15:53, schrieb Manu Evans:

It seems I can't use '-' blocks beneath ':' blocks, for instance:

:javascript
  var x = 0;
  - #{ "var y = " ~ d_variable ~ ";" }
  var arr = [
  - foreach(x; things)
    #{ x ~ "," }
  ];


You get the idea... these don't seem to be getting executed, they're just pasted verbatim into the javascript.
Is there a better approach?

Currently the only way that will work is to use a normal script node and
'|' for the text:

script(type="text/javascript")
	|var x = 0
	|var y = #{d_variable};
	|var arr = [
	- foreach(x; things)
		| #{x},
	|];

The issue with :filters is that runtime and compile time processing get
mixed up when D code gets inserted due to the way they are currently
implemented. That should be improved, but the priority for that so far
is quite low compared to other features/bugs.

Re: :javascript / :css blocks

On Thu, 21 Nov 2013 16:11:16 +0100, Sönke Ludwig wrote:

Am 21.11.2013 15:53, schrieb Manu Evans:

It seems I can't use '-' blocks beneath ':' blocks, for instance:

:javascript
  var x = 0;
  - #{ "var y = " ~ d_variable ~ ";" }
  var arr = [
  - foreach(x; things)
    #{ x ~ "," }
  ];


You get the idea... these don't seem to be getting executed, they're just pasted verbatim into the javascript.
Is there a better approach?

Currently the only way that will work is to use a normal script node and
'|' for the text:

script(type="text/javascript")
	|var x = 0
	|var y = #{d_variable};
	|var arr = [
	- foreach(x; things)
		| #{x},
	|];

Ah, of course. So obvious!

The issue with :filters is that runtime and compile time processing get
mixed up when D code gets inserted due to the way they are currently
implemented. That should be improved, but the priority for that so far
is quite low compared to other features/bugs.

Np.

Re: :javascript / :css blocks

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.

Re: :javascript / :css blocks

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.

Re: :javascript / :css blocks

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)}

Re: :javascript / :css blocks

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.