使用不依赖于NodeJS的EJS。

huangapple go评论65阅读模式
英文:

Using EJS without NodeJS

问题

我想到最新的构建版本将允许我在不使用 Node 的情况下全局使用 ejs,因此我尝试这样做。

然而,当我尝试使用 ejs.renderFile(params...) 时,我遇到了错误:

TypeError:exports.fileLoader 不是一个函数

这只是另一个 Node 模块。有没有办法解决这个问题?

注意:这是我公司唯一使用 EJS 的时候,如果不行的话,您能否友好地指点我如何渲染 .ejs 文件。

英文:

I figured that the latest build release would allow me to use ejs globally without using node so I tried doing so.

Though, when I try to use ejs.renderFile(params...), i get the error:

TypeError: exports.fileLoader is not a function

Which is just another node module. Is there a way to get around this?

Note: This is the only time we use EJS at my company, so, if not, would you kindly point me in a good direction in how to render .ejs files.

答案1

得分: 1

更新:您可以通过运行以下代码创建一个 EJS 对象:

new EJS({url: some/url/to/file.ejs}).render({dataToPass: data, moreData: secondData});
英文:

Update: You can create an EJS object by running

new EJS({url: some/url/to/file.ejs}).render({dataToPass: data, moreData: secondData});

答案2

得分: 0

另一种您可以使用 render 而不是 renderFile 的方式,您的 ejs 文件 可以作为字符串获取:

// 在这里使用 jQuery
$.get(you_ejs_file_path).then(function (str, status, xhr) {
    const html = ejs.render(str, your_data)); // str 是 ejs 文件
});
英文:

yet another way you can use render instead of renderFile, and your ejs file can be get as string:

// here using jQuery
$.get(you_ejs_file_path).then(function (str, status, xhr) {
    const html = ejs.render(str, your_data)); // str is ejs file 
});

huangapple
  • 本文由 发表于 2020年4月8日 20:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定