2013-04-08 205 views
3

我正在使用grunt-ember-templates預編譯我的模板。正如預期的那樣,該工具將我的模板放入Ember.TEMPLATES陣列中。我正在調整grunt-ember-templates的配置。爲此,我想知道Ember.TEMPLATES陣列中的預期關鍵點是什麼。比方說,我有這樣的模板:給定數據模板名稱的匹配模板名稱

<script type="text/x-handlebars" data-template-name="phones/index"> 
    .... 
</script> 

目前,我有一個名爲app/templates/phones_index.hbs文件這個模板,並grunt-ember-templates是把預編譯模板Ember.TEMPLATES["app/templates/phones_index"],但這是錯誤的。

data-template-name="phones/index"的預期關鍵是什麼?

+1

我相信你應該編譯應用程序/ templates'目錄的'內容,而忽略在文件名中的目錄部分,所以你不要將它翻譯成模板名稱。理想情況下'phones/index.hbs'應該翻譯成'App.TEMPLATES [「phones/index」]'。請注意'_'用於部分(例如:'phones/_form.hbs',用於連接到電話資源的通用表單),所以在部分模板以外的模板中使用它可能不是一個好主意。我不是Grunt tho上最好的人,所以我不能說你應該怎麼做。 – MilkyWayJoe 2013-04-08 19:54:54

回答

4

在你的例子中,Ember.TEMPLATES中的密鑰應該是「phones/index」。

可以配置咕嚕-燼模板砸你的路徑的第一部分和app/templates/後留下的一切,這將給你正確的密鑰假設你把你的模板文件app/templates/phones/index.hbs內。使用此設置,app/templates/phones/index.hbs文件的密鑰將爲Ember.TEMPLATES['phones/index'],這與具有data-template-name="phones/index"的未編譯的<script>標記相同。

Gruntfile.js(相同Gruntfile.js在this項目):

ember_templates: { 
    options: { 
    templateName: function(sourceFile) { 
     return sourceFile.replace(/app\/templates\//, ''); 
    } 
    }, 
    'dependencies/compiled/templates.js': ["app/templates/**/*.hbs"] 
}, 
+0

比我想象的更簡單+1 – MilkyWayJoe 2013-04-08 20:11:33

+0

謝謝。然後,我會將所有與手機相關的模板放在「app/templates/phones」目錄中。但後來我遇到了一個問題:在哪裏放置與data-template-name =「phone」'相關的模板,或者放到'data-template-name =「phone/edit」'? – dangonfast 2013-04-08 20:14:25

+0

另一個問題:你說「這與使用data-template-name =」phones/index「」的未編譯的