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?!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.