2015-07-03 51 views
0

我有一個應用程序,有3個頁面,我想自成一體。爲了DRY代碼的興趣,我想爲頁眉和頁腳內容做「包含」。我已經看過grunt-html-build的文檔和示例,並且我不知何故來了。所有HTML位於路徑'src/html'中,包含在名爲「includes」的子文件夾中:'src/html/includes'使用Grunt的基本包括

下面是HTML樣本:

<!doctype html> 
<html> 
    <head> 
    <!-- build:section head --><!-- /build --> 
    </head> 

    <body> 
    <p>A bunch of unique content for each of the pages</p> 

    <!-- build:section footer --><!-- /build --> 
    </body> 
</html> 

然後在我的我有以下幾點:

htmlbuild: { 
    src: 'src/html/app-*.html', 
    dest: 'dist/', 
    options: { 
    sections: { 
     head: 'src/html/includes/head.html', 
     footer: 'src/html/includes/footer.html' 
    } 
    } 
} 

我敢肯定,這只是語法,但我似乎無法得到過去的這個錯誤:

Warning: an error occurred while processing a template (Unexpected identifier).

張女士說,它這是一個「意外的標識符」,告訴我,我不是在打點「我」或正確穿過「t」。感謝更有經驗的眼睛!

注意:我已經考慮使用來代替,但如果沒有globbing,我將不得不做3個獨立的任務,以保持獨特的內容不變。


[編輯補充:]

我有成功使用稱爲(適當地)grunt-includes不同咕嚕任務我很基本的使用情況。我能夠適當地包含我的文件。

但是,我仍然對有興趣有條件地構建開發包或分發包。任何洞察力仍然感激!

回答

1

htmlbuild是一個多任務,所以你需要定義一個目標下的文件:

module.exports = function(grunt) { 
    grunt.initConfig({ 
     htmlbuild: { 
      dist: { 
       src: 'src/html/app-*.html', 
       dest: 'dist/', 
       options: { 
        sections: { 
         head: 'src/html/includes/head.html', 
         footer: 'src/html/includes/footer.html' 
        } 
       } 
      } 
     } 
    }); 
    grunt.loadNpmTasks('grunt-html-build'); 
    grunt.registerTask('default', ['htmlbuild']); 
}; 
+0

感謝您抽空回覆的時候了!我直接從他們的文檔中獲得了代碼。以下是我提供的主要示例:https://github.com/spatools/grunt-html-build/blob/master/docs/sections.md,它表明它不必擁有目標。就這樣吧,我給了它一個回報,它返回了這個錯誤:'不能讀取undefined'的屬性'dest'。我很驚訝,我找不到更多的「基本包含」功能的文章或樣本! –

+0

嗨,我用我的機器上成功測試過的確切Gruntfile更新了我的答案。你可以在你身邊嘗試嗎? –

+0

另外,我嘗試沒有定義目標,我得到了一個錯誤。 Target('dist' here)**是強制性的 - 我猜grunt-html-build文檔有一些問題。 –