2013-10-08 29 views
2

對於集合我想使用docpad.coffee中定義的助手如如何設置docpad.coffee輔助函數以進行集合生成?

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

,但不能得到FilesCollection知道我的助手:/

如何變爲可用設置輔助功能爲收藏定義?

回答

3

參考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 }) 
} 
0

Mh,而不是(最)優雅的方式,但使用docpad.coffee中的絕對路徑,我可以參考它。如果在templateData中定義,則爲docpadConfig.templateData.getLang(...)