Hammereditor2
Hammereditor2

Reputation: 63

Node.js Jade: Using conditionals within JavaScript

I am making a Node.js web page with the Jade template engine. I need to create rows and columns inside a table, depending on a value which is inputted:

- var gpu_count = #{GPU_count};
          for (var g = 0; g < gpu_count; g++)
            tr
              - for (var v = 0; v < 15; v++)
                td.vcardTextValue somevaluegoeshere

#{GPU_count} is a number which I am inputting to the Jade document. I want the number of iterations of the for-loop to be equal to gpu_count. But unfortunately, when I load the page, this error appears:


Error
    at new JS_Parse_Error (C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:196:18)
    at js_error (C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:204:11)
    at parse_error (C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:296:9)
    at Object.next_token [as input] (C:\Users\Anna\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:528:9)
    at next (C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:622:25)
    at vardefs (C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:1024:48)
    at var_ (C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:1037:27)
    at C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:794:30
    at C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:683:24
    at block_ (C:\Users\whoever\Documents\nodejs\node_modules\jade\node_modules\with\node_modules\uglify-js\lib\parse.js:944:20)
I believe this is because of a syntax error; I cannot place #{GPU_count} inside of the JavaScript. So how would I make a JavaScript variable equal to an input value?

Upvotes: 1

Views: 165

Answers (1)

Rob Raisch
Rob Raisch

Reputation: 17387

If the variable is in the same scope as the JavaScript, just use it's name, as in:

var gpu_count = GPU_count;

If the value you need is stored on the page in an input element, as in:

<input id="GPU_count"...>

You'll need to use something to send the value from the page to the server (where jade is running) like AJAX, socket.io or similar.

Upvotes: 1

Related Questions