通过 Gulp Task 将变量注入 html [英] Inject variable into html via Gulp Task

查看:15
本文介绍了通过 Gulp Task 将变量注入 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Gulp 和一些插件来模板化我的 HTML 并缩小我的 HTML 即gulp-file-include"、gulp-inject"、gulp-minify-html"似乎没有提供一种方法在构建时注入一个常量,例如:

I'm using Gulp and a number of plugins to template my HTML and minify my HTML i.e. "gulp-file-include", "gulp-inject", "gulp-minify-html" none seem to offer a way of injecting a constant at build time i.e something like this:

.pipe('*.html', $.gulpVar({urlpath: 'www.myurl.com'})))

.pipe('*.html', $.gulpVar({urlpath: 'www.myurl.com'})))

然后在我的 HTML 中引用变量

then in my HTML a reference to the variable

联系我们

我能找到的最接近类似的东西是 gulp-ng-constant 虽然这会将其注入 JS 并使用 Angular,但我没有使用 Angular 并希望将其注入 HTML.有没有具有该功能的 gulp 插件?

The closest I can find to something similar is gulp-ng-constant although this injects it into the JS and uses Angular, but I'm not using Angular and wish to inject it into the HTML. Is there a gulp plugin that has that feature?

推荐答案

你可以使用 gulp-replace

you can use gulp-replace

gulp.task('templates', function(){
  gulp.src('template.html')
    .pipe(replace(/$$myvar$$/g, 'release'))
    .pipe(gulp.dest('build/file.txt'));
});

上面的任务是用 template.html

这篇关于通过 Gulp Task 将变量注入 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆