is there a way to add CSS/JS later using EJS with nodejs/express-Collection of common programming errors

found a solution here: Node.js with Express: Importing client-side javascript using script tags in Jade views?

it’s using jade instead of EJS but works all the same. here are some code-snippets for express 2.4.0.

you have to add the following “helpers” to your app.js

app.helpers({
  renderScriptsTags: function (all) {
    if (all != undefined) {
      return all.map(function(script) {
        return '';
      }).join('\n ');
    }
    else {
      return '';
    }
  }
});

app.dynamicHelpers({
  scripts: function(req, res) {
    return ['jquery-1.5.1.min.js'];
  }
});

the layout.ejs looks sth like this:



  
    
      
      
  
  
    
  

if you don’t add any scripts to the scripts-array, only ‘jquery-1.5.1.min.js’ will be included – if you want to add files to a subpage you can do this like so:

test.ejs




I'm a template with 3 js files in the header

that's it.

Originally posted 2013-11-09 20:50:42.