我正在阅读Journo的source code,我偶然发现了这行代码:
markdown = _.template(source.toString()) variables
variables
在这做什么? _.template(source.toString()) variables
是否有效的stntax?
这是包装该行代码的函数:
Journo.render = (post, source) ->
catchErrors ->
do loadLayout
source or= fs.readFileSync postPath post
variables = renderVariables post
markdown = _.template(source.toString()) variables
title = detectTitle markdown
content = marked.parser marked.lexer markdown
shared.layout _.extend variables, {title, content}
答案 0 :(得分:1)
是的,它是有效的。在调用函数时,括号在CoffeeScript中是可选的(有时),因此它采用template
的结果并使用参数调用它。它编译为这个JavaScript:
_.template(source.toString())(variables);
来自CoffeeScript documentation:
如果要传递参数,则不需要使用括号来调用函数。隐式调用包装到行或块表达式的末尾。
答案 1 :(得分:1)
_.template
编译source.toString()
指定的模板。模板是一个函数,然后被调用。 variables
是该函数的参数(就像postPath post
} fs.readFileSync
的参数一样。
另请参阅_.template
的文档答案 2 :(得分:-1)
这个问题得到了很好的回答,但为了帮助OP进行未来的咖啡特技,一个很好的方法来解决这些公案
我承认有时会对coffeescript感到困惑,这是绝对的..并且可以省去头痛。