3

i was wondering how to execute js before rendering?

This fails

-#{somejs} // Outputs just the js-code
p #{somejs()} // Executes the js-code, but doenst render the html

// In EJS I just write. But how can i do this with node?

<%- somejs() %>

// I try to use express-messasges (https://github.com/visionmedia/express-messages) with Jade instead of ejs

fabian
  • 5,433
  • 10
  • 61
  • 92
  • If the processing is complex, wouldn't it be better to execute your javascript before rendering the jade template? Such as, in the route that calls `res.render()`. For simple things like alternate row colouring calculations, inline `- blah()` does the trick. – Mike Causer Mar 12 '13 at 02:03

3 Answers3

7

The following both work for me:

- var test = Math.sqrt(16);
  div #{test}

or

div #{Math.sqrt(25)}

If possible, would you please post your somejs()? Are you certain that it is producing a HTML string?

AndreiM
  • 4,558
  • 4
  • 36
  • 50
3

To use express-messages with jade use this code in your template:

- var m = messages()
!= m
Florian Fankhauser
  • 3,615
  • 2
  • 26
  • 30
2

In jade simply use - someJS() for more example see the jade website and the in the readme

errorhandler
  • 1,757
  • 3
  • 22
  • 29
  • BTW: I try to use express-messasges (https://github.com/visionmedia/express-messages) with Jade instead of ejs – fabian Apr 26 '11 at 07:21