在Node.JS中使用ejs(模板)为星星生成这种html的最干净,最有效(在打字方面)的方法是什么?
星星看起来像:
<div class="ratings">
<p class="pull-right"> reviews</p>
<p>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star-empty"></span>
</p>
</div>
我试图避免使用这样一些恶心的语法:
<div class="ratings">
<p class="pull-right"> reviews</p>
<p>
<% for (i = 0; i < 5; i++) { %>
<% if (i < (star_rating - 0.5)) { %>
<span class="glyphicon glyphicon-star"></span>
<% } else { %>
<span class="glyphicon glyphicon-star-empty"></span>
<% } %>
<% } %>
</p>
</div>
答案 0 :(得分:0)
你对for循环无能为力,但使用ternary/conditional运算符可以将其中的代码减少4行:
<div class="ratings">
<p class="pull-right"> reviews</p>
<p>
<% for (var i = 0; i < 5; i++) { %>
<span class="glyphicon glyphicon-star<%= (i < star_rating - 0.5)? '' : '-empty'%>"></span>
<% } %>
</p>
</div>