node.js - 在 jade 中导入 CSV/JSON

标签 node.js pug

我使用 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/

相关文章:

javascript - 如何将用户创建的复选框值传递到另一个页面?

node.js - NodeJS MongoDB mongoose db.open 不起作用

linux - node.js 找不到模块 xml2js

javascript - 在 before() 中使用 .then 时无法预期脉轮中的状态

java - 将 javaFX 客户端连接到 NodeJs 服务器

node.js - node.js 垃圾收集的时机

javascript - Jade 包含基于变量的模板

vue.js - 从 Vue 脚本中获取 Pug/Jade 混合参数

angular - 有没有办法在 Angular 4 中使用 "template reference variable"?

javascript - 我的 $(document).ready 里面什么都没有