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

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

问题描述

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

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



然后在我的HTML中引用变量

联系我们



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

解决方案

您可以使用gulp-replace

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

上面的任务正在替换 $$ myvar $$ by release in template.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'})))

then in my HTML a reference to the variable

Contact Us

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?

解决方案

you can use gulp-replace

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

above task is replacing $$myvar$$ by release in template.html

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

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