I am want to add a separate CSS file for each view in ejs
this is my project tree:
My-App
├─ app.js
├─ package-lock.json
├─ package.json
├─ public
│ └─ css
│ └─ style.css
└─ views
├─ header.ejs
├─ view1.ejs
├─ view2.ejs
├─ view3.ejs
└─ footer.ejs
All of these views are styled by only one file which is style.css. I link this CSS file in header.ejs as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../css/style.css" type="text/css">
<title>XXXXXX</title>
</head>
<body>
<div >
<div >
<form action="/" method="post">
<div >
<button name="leftBtn" value="XXX1" type="submit">XXX</button>
<button name="leftBtn" value="XXX2" type="submit">XXX</button>
<button name="leftBtn" value="XXX3" type="submit">XXX</button>
<button name="leftBtn" value="XXX4" type="submit">XXX</button>
</div>
</form>
</div>
and of course in each view I do the following
<%- include("header") -%>
<!-- the view content -->
<%- include("footer") -%>
what I do want is to do something like <link rel="stylesheet" href="../css/view1Style.css" type="text/css">
in each view file
CodePudding user response:
Instead of using two partials, use three. Put all similar body code in another partial.
//menu.ejs
<div >
<div >
<form action="/" method="post">
<div >
<button name="leftBtn" value="XXX1" type="submit">XXX</button>
<button name="leftBtn" value="XXX2" type="submit">XXX</button>
<button name="leftBtn" value="XXX3" type="submit">XXX</button>
<button name="leftBtn" value="XXX4" type="submit">XXX</button>
</div>
</form>
</div>
Then change other view structure like this:
<%- include("header.ejs") -%>
<!-- Add your separate css files here -->
<link rel="stylesheet" href="../css/view1Style.css" type="text/css">
</head>
<body>
<%- include("menu.ejs") -%>
<!-- the view content -->
<%- include("footer.ejs") -%>
<!-- You can also add separate script files here -->
</body>
</html>