Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to add CSS/JS later using EJS with nodejs/express

i'm using the EJS template engine with nodejs/express and i'm wondering if it's possible to add another css or js file in e.g the index.ejs (not the layout.ejs)

layout.ejs

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <link rel='stylesheet' href='/stylesheets/smoothness/jquery-ui-1.8.14.custom.css' />
  </head>
  <body>
    <%- body %>
  </body>
</html>

index.ejs

<h1><%= title %></h1>
<p>Welcome to <%= title %></p>

i don't want to add the second css file in every template but only the index.ejs - is there any way i can do that?

like image 924
Philipp Kyeck Avatar asked Jul 07 '11 10:07

Philipp Kyeck


People also ask

How to create a node app with Express and EJS?

Initialize a new Node project in the folder by running npm init -y in the terminal, then to install Express and EJS, run: After installation, create an app.js file and a views folder in the root folder. Inside the views folder, create two folders pages and partials; I will be explaining why we need these folders shortly.

How to add CSS to an EJS file?

And for adding CSS to your EJS file, you have to use "public" folder (or any other name, name doesn't matter) from which you can serve static file like CSS, JS or images Show activity on this post. Thanks for contributing an answer to Stack Overflow!

How to use EJS as the view template engine of express?

This tutorial assumes the use of EJS as the view template engine of your Express app. The easiest way to create an Express app is by using the express-generator. In this example, we're going to use MaterializeCSS to beautify our app. Go to MaterializeCSS's website and download the compressed CSS and JavaScript files.

How do I get Started with Node JS?

It’s minimalistic and easy to get started with. Let’s start a project from scratch. Create a new folder where you want to put the project files. Initialize a new Node project in the folder by running npm init -y in the terminal, then to install Express and EJS, run:


2 Answers


In app.js add line:

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public')); // This line.

In layout.ejs:

<!DOCTYPE html>
<html>
  <head>
    <title>Authentication Example</title>
    <link rel="stylesheet" href="/stylesheets/style.css"/>
    <script src="/javascripts/jquery.js"></script>    
  </head>
  <body>
    <%- body %>
  </body>
</html>

In index.ejs or login.ejs:

<h1>Login</h1>
<form method="post" action="/login">
  <p>
    <label>Username:</label>
    <input type="text" name="username">
  </p>
  <p>
    <label>Password:</label>
    <input type="text" name="password">
  </p>
  <p>
    <input type="submit" value="Login">
  </p>
</form>
<script src="/javascripts/test.js"></script> <!-- Second Script -->

In test.js:

$(document).ready(function() {
    try{
        alert("Hi!!!");
    }catch(e)
    {
        alert("Error");
    }
});

Regards.

like image 178
alditis Avatar answered Oct 12 '22 21:10

alditis


Thanks @asprotte for providing this for express 4.x. Did you tested this? Because it does not appears to be working for me. So I have made some changes to your code here are they:

Put this in app.js file

app.locals.scripts = [];
app.locals.addScripts=function (all) {
app.locals.scripts = [];
    if (all != undefined) {
        app.locals.scripts =  all.map(function(script) {
            console.log(script);
            return "<script src='/js/" + script + "'></script>";
        }).join('\n ');
    }

};
app.locals.getScripts = function(req, res) {
    return app.locals.scripts;
};

then in template file put (I am using ejs template here) :

<% addScripts(['cart.js']) %>

Then in the layout file we need these to append at the bottom of the page get the scripts

<%- getScripts() %>

I have tested it and its working for me. Please correct me if I am wrong.

Thanks,

like image 24
Mohsin Khan Avatar answered Oct 12 '22 21:10

Mohsin Khan