Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

html - How to add conditional css in ejs layout

I have ejs layout like this on my views/layout.ejs

<html>
<head>
<title>title</title>
</head>
<body>
<%- body %> 
</body>
</html>

Then, on my views/login/login.ejs I have.

<h1 class="login">Login</h1>

I'm trying to link css file with my login.ejs file, but how I do that? I've already read this, It says that we should use conditional statement in layout.ejs but I don't really understand since it's wrote in asp language.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To conditionally add CSS, you simple use plain JavaScript within EJS tags:

<head>
   <title>title</title>
   <% if (condition) { %>
     <link rel="stylesheet" href="/path/to/main.css">
   <% } %>
</head>

/path/to/main.css is resolved by your server, not EJS. condition can be anything JavaScript can do. For example:

<% if (path === '/login') %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...