我使用 jade 作为静态网站的模板引擎,所以我没有使用任何 express 服务器。 我想导入一个 csv 或 json 文件以将数据加载到我的模板中。使用快速服务器可以很容易地完成,但我不知道没有服务器该怎么做。
作为临时解决方案,我像这样手动加载数据:
- var arr = []
- arr.push(['hello', 'world', 'foo', 'bar'])
- arr.push(['hello1', 'world1', 'foo1', 'bar1'])
- arr.push(['hello2', 'world2', 'foo2', 'bar2'])
- arr.push(['hello3', 'world3', 'foo3', 'bar3'])
- arr.push(['hello4', 'world4', 'foo4', 'bar4'])
- for (var i; i < arr.length; ++i) {
div
h1=arr[i][0]
// some more jade
- }
相反,我正在寻找这样的东西:
- var arr = require('./data.csv')
- for (var i; i < arr.length; ++i) {
div
h1=arr[i][0]
// some more jade
- }
有什么办法可以在 jade 中做到这一点吗?
非常感谢
最佳答案
来自Jade Docs :
在 Node 中:
var list = require('./list.json'),
jade = require('jade');
// Compile a function
var fn = jade.compileFile('./index.js', options);
// Render the function
var html = fn(list);
// serve html response however you currently serve rendered jade
locals 将是您导入的对象。
关于node.js - 在 jade 中导入 CSV/JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29966972/