语法问题通过EJS函数中的HTML
我想根据我的 mongo 数据库中存储的评级循环一个star svg。我单独测试了该函数,所以我知道它应该可以工作,但是在将函数插入我的 ejs 页面并将 html 添加到函数中时遇到语法问题。
这是我当前的功能。错误通常会显示“意外的令牌 ';'” 或“意外的令牌 ')'”,具体取决于我尝试格式化 ejs 以使一切正常工作的方式。我觉得我的格式只是让 ejs 无法正确读取它,但我无法找出问题所在。
<% function repeatStringNumTimes(string, times) {%>
<% return string.repeat(times)};%>
<%repeatStringNumTimes(%>
<%= <i class="bi-star-fill" style="font-size: 1rem; color: #FD4;"></i> %>
<%, review.rating); %>
这是下面的测试函数,无需使用 ejs 或实现 html 即可工作。
function repeatStringNumTimes(string, times) {
console.log(string.repeat(times));
}
repeatStringNumTimes(`a`, review);
我已经在没有 ejs 和 html 的情况下测试了该功能。我尝试了一些不同的 ejs 标签 - 说实话,我对这些标签以及在哪里使用它们没有最好的理解。我可能在 html 中使用了错误的标签。我肯定有正确的对象字符串等用于调用数据库,以及带有我已经测试过的类的 svg,并且它们可以正常工作。
已解决-使用变量来传递 html 字符串。使用 <%- 标签来传递实际被调用的函数。
<!-- GOT IT TO WORK!! -->
<% let str=`<i class="bi-star-fill" style="color: #FD4;"></i>`;%>
<%function repeatStringNumTimes(str, times) { %>
<%return str.repeat(times); %>
<% } %>
<%- repeatStringNumTimes(str, review.rating); %>
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 class="bi-star-fill" 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 class="bi-star-fill" style="color: #FD4;"></i>`;%>
<%function repeatStringNumTimes(str, times) { %>
<%return str.repeat(times); %>
<% } %>
<%- repeatStringNumTimes(str, review.rating); %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试修复I-TAG; I guess "review.rating" is the falue for your i-tag:
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 ?
try to fix the i-tag; I guess "review.rating" is the falue for your i-tag:
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?
已解决-使用一个变量来传递 html 字符串(这似乎主要允许我缩短函数中每行的长度,因为我的格式化程序不喜欢我的 ejs 代码并且会错误地重新格式化)。我使用 <%- 标签来传递实际调用的函数。
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.