Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass variable from jade template file to a script file?
    primarykey
    data
    text
    <p>I'm having trouble with a variable (config) declared in a jade template file (index.jade) that isn't passed to a javascript file, which then makes my javascript crash. Here is the file (views/index.jade):</p> <pre><code>h1 #{title} script(src='./socket.io/socket.io.js') script(type='text/javascript') var config = {}; config.address = '#{address}'; config.port = '#{port}'; script(src='./javascripts/app.js') </code></pre> <p>Here is a part of my app.js (server side):</p> <pre><code> app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); app.configure('development', function(){ app.set('address', 'localhost'); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function(){ app.use(express.errorHandler()); }); // Routes app.get('/', function(req, res){ res.render('index', { address: app.settings.address, port: app.settings.port }); }); if (!module.parent) { app.listen(app.settings.port); console.log("Server listening on port %d", app.settings.port); } // Start my Socket.io app and pass in the socket require('./socketapp').start(io.listen(app)); </code></pre> <p>And here is a part of my javascript file that crashes (public/javascripts/app.js):</p> <pre><code>(function() { var socket = new io.Socket(config.address, {port: config.port, rememberTransport: false}); </code></pre> <p>I'm running the site on development mode (NODE_ENV=development) on localhost (my own machine). I'm using node-inspector for debugging, which told me that the config variable is undefined in public/javascripts/app.js.</p> <p>Any ideas?? Thanks!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload