我的问题类似于发布的 here .本质上我想读一个配置file.json
看起来像这样:
{
"message": "Error in #{baseName} at #{directory}"
}
我想使用作为 map /对象提供的变量来插入消息字符串。与上面的问题不同,要格式化的字符串在编译时是未知的。
是否可以使用 CoffeeScript 中的内置方法运行字符串插值,或者我是否必须使用 mustache 或类似技术?
最佳答案
你可以很容易地使用 CoffeeScript 模板文字连接一个非常基本的模板引擎。
您必须将 JSON 定义为导出的 CS 文件:
module.exports = (context)->
[
some: "#{context.dynamic.content}"
,
some: 'other static content'
]
然后,您只需使用所需的值调用该函数:
render = require './data'
myContext =
dynamic: content: 'some dynamic content'
dynamicData = render(myContext)
此时
dynamicData
看起来像这样:[
some: 'some dynamic content'
,
some: 'other static content'
]
关于coffeescript - CoffeeScript 中的字符串插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45223366/