0

Is there way to file (css/js) call based on specific page condition in EJS, Or any flag using from router or condition based on URL in EJS file

Note: Below code is working fine when calling /editor page but it will break if go other page because of undefined 'isEditorPage' variable . I think this practice is bad idea So I am searching for perfect way for this situation

It is a common header - ejs file

<head>
<link rel="stylesheet" href="common.css">
 <%if (isEditorPage) { %>
<link rel="stylesheet" href="../css/admin/editor.css">
 <% } %>
</head>

Express router

router.get('/editor', function(req, res){
    let isEditorPage = true;
    Blog.find({}, function(err, blog){
        res.render('admin/blog', {isEditorPage} );
    });
});
ShibinRagh
  • 6,530
  • 4
  • 35
  • 57
  • 1
    Try `{isEditorPage: isEditor}` or rename the variable `let isEditorPage = true;` – Titus Aug 30 '18 at 10:15
  • Thats is my typo error here , its working but it will be error on other page because of undefined isEditorPage variable , so Im asking for best way to do this – ShibinRagh Aug 30 '18 at 10:18
  • Possible duplicate of [What is the proper way to check for existence of variable in an EJS template (using ExpressJS)?](https://stackoverflow.com/questions/5372559/what-is-the-proper-way-to-check-for-existence-of-variable-in-an-ejs-template-us) – Titus Aug 30 '18 at 10:27
  • @TGrif Brother Success , Please Answer this I can mark your answer – ShibinRagh Aug 30 '18 at 10:37

1 Answers1

0

try typeof:

<%if (typeof isEditorPage !== 'undefined') { %>
<link rel="stylesheet" href="../css/admin/editor.css">
<% } %>
JKong
  • 254
  • 1
  • 8