首页 > 解决方案 > Underscorejs 使用 EJS 随机播放

问题描述

使用https://underscorejs.org/#shuffle

是否可以_.shuffle(list)与 EJS 一起使用?

我得到 _ 没有定义。

<script type="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

<% var breakfastFoods = []  %>
<% var breakfastFoodsInfo = []  %>
<% for (var food of allDBFoods) { %>
<% if (food.breakfast == true ){ %>
<% breakfastFoods.push(food.name) %>
<% breakfastFoodsInfo.push(food.info) %>
<% } %>
<% } %>


<% _.shuffle([breakfastFoods]); %>

标签: arraysnode.jsunderscore.jsejs

解决方案


我想这个过程是相似的,但这就是我moment在我的 EJS 文件模板中使用的方法

# routes.js

const moment = require('moment');


app.get('/fixtures', (req, res) => {
  const file = await readFile('./views/partials/fixtures.ejs');
  const fixtureTemplate = ejs.compile(file, { client: true });
  const html = fixtureTemplate({ moment });
  res.send({ html });
});

在我的examples.ejs文件中,我可以像这样使用时刻

<%=  moment.utc(fixture.kick_off).local().format('HH:mm') %>

希望您可以遵循相同的使用过程underscore


推荐阅读