2016-04-17 65 views
0

我有一個index.html和一個header.html。我想要做的是完成一個吞嚥任務,從header.html獲取文本並將其插入到index.html中。因此,例如:我可以使用gulp將文件中的內容插入另一個文件嗎?

了header.html

abcdef 
ghijkl 

的index.html之前插入

line1 
line2 
<!-- header:start --> 
... 
... 
... 
<!-- header:end --> 
line3 

的index.html插入後

line1 
line2 
<!-- header:start --> 
abcdef 
ghijkl 
<!-- header:end --> 
line3 
line4 

誰能幫助我,給我一些建議我如何做到這一點?

回答

1

你的事後肯定是可以實現的。最快和最簡單的可能是使用一個衆所周知的誘人的解決方案就像https://www.npmjs.com/package/gulp-mustache & http://mustache.github.io/mustache.5.html

它看起來像

的src /路由/ index.html的

<html> 
{{> ../partials/head.html }} 
</html> 

的src /諧音/頭。 HTML

<head> 
    <link .... /> 
    <somethingelse> 
</head> 

一飲而盡文件

gulp.src("./src/routes/*.html") 
    .pipe(mustache()).pipe(gulp.dest("./dist")); 

應該有希望產生

DIST/index.html的

<html> 
    <head> 
    <link .... /> 
    <somethingelse> 
    </head> 
</html> 

雖然我還沒有嘗試過這些片段完全是,我認爲它應該傳達的想法:)

做純JS & gulp也是可能的,它可能需要一個自定義的流處理器和某種搜索替換等,但除非你有一個不使用外部依賴的理由,我會建議誘人的庫你將安裝獲得更多的功能(循環等)

相關問題