-6ren">
gpt4 book ai didi

docpad - 如何为集合生成设置 docpad.coffee 辅助函数?

转载 作者:行者123 更新时间:2023-12-04 04:38:46 26 4
gpt4 key购买 nike

对于集合,我想使用 docpad.coffee 中定义的助手,例如

getLang: (inLang) ->
"/" + inLang + "/"
...
myCollection: ->
@getCollection("html").findAllLive().on "add", (model) ->
model.setMeta({ url: @getLang("en") + defaultUrlPartGoesHere })

但无法让 FilesCollection 知道我的助手:/

如何设置辅助函数以可用于集合定义?

最佳答案

引用 docpadConfig.templatedata.getLang()会起作用,但如果你觉得那令人反感,请记住 docpad.coffee 只是一个标准的 NodeJS 模块(用 coffeescript 编写)。您还可以在 docpadConfig 文字对象之外定义您的函数,然后将其拉入您的 templateData(假设您需要它用于模板)并在构建集合时使用它。

例如:

# define the function outside of the config object
getLang: (inLang) ->
"/" + inLang + "/"

docpadConfig = {
templateData:
getLang: getLang # reference the previously defined function

collections:
myCollection: ->
# use the previously defined function
@getCollection("html").findAllLive().on "add", (model) ->
model.setMeta({ url: getLang("en") + defaultUrlPartGoesHere })
}

关于docpad - 如何为集合生成设置 docpad.coffee 辅助函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19247026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com