0

I'm using express and ejs to render my files. In a addition I render a <%= data.groupID %> which is 60113 like this:

<div>
    <%= data.groupID %>
</div>

<div>
    <% include docs/profile_60113 %>
</div>

How can I use the variable in the <% include docs/profile_GROUPID %> expression?!

<div>
    <script>
     var getGroupID;
     function functiongroupID() {
         getGroupID = '<%= data.groupID %>';
     }
     functiongroupID();
     console.log(getGroupID);

 </script>
</div>

<div>
    <%= data.groupID %>
</div>

<div>
    <% include docs/profile_GROUPID %>
</div>  

Is that even possible or do I need to do an if?!

Philipp M
  • 3,306
  • 5
  • 36
  • 90

1 Answers1

1

The syntax for an include is:

<%- include('user/show', {user: user}); %>

You can just put the variable where you would normally put the string literal.

<%- include(variable_here, {user: user}); %>

And if you want to combine it with a string, you do so in any of the normal ways:

<%- include("docs/profile_" + GROUPID); %>
<%- include(`docs/profile_${GROUPID}`); %>

Obviously, GROUPID needs to be a variable generated in your server-side code and not in your client-side code since your client side <script> element won't run until the EJS has finished executing and the output of it sent to the browser.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335