Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

node.js - How to share varibles between ejs files

I am building a website and want to conditionally render a given page. For that I have created this condition

    <% let view_condition = (allowed_roles.includes(main_user.role) && meeting[enabled_property]) || main_user.role === 'admin' %>

this works fine for me. But I want this condition in a partial and I want to pass allowed roles and enabled_property to it. So I created a partial and included it in the main.ejs file but it showed the error that view_condition is not defined.

I tried using both tags but it did not work for me <%- include('./partials/view_condition') %> and <% include('./partials/view_condition') %>.

I know the path is correct because when I use a h1 tag in view_condition.ejs it renders properly

main.ejs file (that is not working)

<%- include('./apartials/view_condition',{allowed_roles : ['crew','client'],enabled_property : 'call_in_enabled'}) %> 
<% if (view_condition) { %>
  <!-- application code-->
  <% } else{ %>
    <div style="position: absolute;width: 100%;top: 50vh;display: flex;justify-content: center;">
      <h1> you are not authorized to view this page</h1>      
    </div>
  <% } %> 

view_condition.ejs file (I tried using both let and var to declare varible)

<% let view_condition = (allowed_roles.includes(main_user.role) && meeting[enabled_property]) || main_user.role === 'admin' %>

main.ejs file (that is working)

<% let allowed_roles = ['admin', 'crew','client'] %> 
 <% let enabled_property = 'call_in_enabled' %> 
<% let view_condition = (allowed_roles.includes(main_user.role) && meeting[enabled_property]) || main_user.role === 'admin' %>  
<% if (view_condition) { %>
  <!-- application code-->
  <% } else{ %>
    <div style="position: absolute;width: 100%;top: 50vh;display: flex;justify-content: center;">
      <h1> you are not authorized to view this page</h1>      
    </div>
  <% } %> 
question from:https://stackoverflow.com/questions/65948782/how-to-share-varibles-between-ejs-files

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...