I want to loop a star svg based on the stored rating in my mongo database. I tested the function separately so I know it should work, but I am having syntax issues with inserting the function into my ejs page and adding the html into the function.
This is my current function. The errors generally say "Unexpected token ';'" or "Unexpected token ')'" depending on how I try to format the ejs to make everything work. I feel like my formatting is simply confusing ejs into reading it improperly but I can't figure out the issue.
<% function repeatStringNumTimes(string, times) {%>
<% return string.repeat(times)};%>
<%repeatStringNumTimes(%>
<%= <i style="font-size: 1rem; color: #FD4;"></i> %>
<%, review.rating); %>
This is the test function below that works without ejs or the html implemented.
function repeatStringNumTimes(string, times) {
console.log(string.repeat(times));
}
repeatStringNumTimes(`a`, review);
I've tested the function without ejs and html. I've tried a few different ejs tags - to be honest I don't have the best understanding of the tags and where to use them all. I could be using the wrong tags for the html. I definitely have the right object strings and such for calling the database, and the svg with the classes I have tested and they work properly.
SOLVED- used a variable to pass through the html string. used the <%- tag to pass through the actual called function.
<!-- GOT IT TO WORK!! -->
<% let str=`<i style="color: #FD4;"></i>`;%>
<%function repeatStringNumTimes(str, times) { %>
<%return str.repeat(times); %>
<% } %>
<%- repeatStringNumTimes(str, review.rating); %>
CodePudding user response:
try to fix the i-tag; I guess "review.rating" is the falue for your i-tag:
<i style="font-size: 1rem; color: #FD4;"> <%= review.rating %> </i>
so far I understand, your "review.rating" returns a value from 1 to 5, depending on that value you want to populate your ejs file with stars, right?
<% for ( let i = 1; i <= <%= review.rating %> ; i ){ %>
<i style="font-size: 1rem; color: #FD4;"></i>
<% }; %>
CodePudding user response:
SOLVED- used a variable to pass through the html string (this seemed to mainly allow me to shorten the length of each line in the function, as my formatter didn't like my ejs code and would reformat improperly). I used the <%- tag to pass through the actual called function.
<!-- GOT IT TO WORK!! -->
<% let str=`<i style="color: #FD4;"></i>`;%>
<%function repeatStringNumTimes(str, times) { %>
<%return str.repeat(times); %>
<% } %>
<%- repeatStringNumTimes(str, review.rating); %>