简体   繁体   中英

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.

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') %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM