【问题标题】:How to setup docpad.coffee helper functions for collection generation?如何为集合生成设置 docpad.coffee 辅助函数?
【发布时间】:2013-10-08 11:54:24
【问题描述】:

对于收藏,我想使用在 docpad.coffee 中定义的帮助器,例如

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

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

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

【问题讨论】:

    标签: docpad


    【解决方案1】:

    引用docpadConfig.templatedata.getLang() 会起作用,但如果你觉得这很反感,请记住 docpad.coffee 只是一个标准的 NodeJS 模块(用咖啡脚本编写)。您还可以在 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 })  
    }
    

    【讨论】:

      【解决方案2】:

      嗯,不是(最)优雅的方式,但使用 docpad.coffee 中的绝对路径,我可以使用例如引用它docpadConfig.templateData.getLang(...) 如果在 templateData 中定义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-12
        • 1970-01-01
        • 1970-01-01
        • 2020-10-21
        相关资源
        最近更新 更多