如何将变量从gulp传递到javascript或coffeescript资产

如何将变量从gulp传递到javascript或coffeescript资产,javascript,coffeescript,gulp,Javascript,Coffeescript,Gulp,我在前端项目中使用Coffeescript,到目前为止我一直在使用Middleman,我想改用Gulp。有了Middleman,我可以做这样的事情: if foo is "<%= bar %>" # do something doctype html html head title = project_title body #content - if html5? p Routes look like "example.com

我在前端项目中使用Coffeescript,到目前为止我一直在使用Middleman,我想改用Gulp。有了Middleman,我可以做这样的事情:

if foo is "<%= bar %>"
  # do something
doctype html
html
  head
    title = project_title
  body
    #content
      - if html5?
        p Routes look like "example.com/some/route"
      - else
        p Routes look like "example.com/#!/some/route"

    #footer
      | Copyright © #{year} #{author}
var gulp = require('gulp');
var template = require('gulp-template');
var slim = require('gulp-slim');

gulp.task('slim', function () {
  return gulp.src('src/*.slim')
    .pipe(template({
      projectTitle: 'Example',
      year: '2015',
      author: "Master Yoda"
      isHtml5: function(){
        // ...
      }
    }))
    .pipe(slim({pretty: true}))
    .pipe(gulp.dest('dist'));
});
其中,bar是config.rb中的一个变量。我想用大口喝做同样的事情。有没有办法将变量和/或函数从gulpfile传递到脚本资产?

我最终使用了gulp模板,该模板的编译看起来与erb非常相似。让我们举一个slim模板的例子,它通常如下所示:

if foo is "<%= bar %>"
  # do something
doctype html
html
  head
    title = project_title
  body
    #content
      - if html5?
        p Routes look like "example.com/some/route"
      - else
        p Routes look like "example.com/#!/some/route"

    #footer
      | Copyright © #{year} #{author}
var gulp = require('gulp');
var template = require('gulp-template');
var slim = require('gulp-slim');

gulp.task('slim', function () {
  return gulp.src('src/*.slim')
    .pipe(template({
      projectTitle: 'Example',
      year: '2015',
      author: "Master Yoda"
      isHtml5: function(){
        // ...
      }
    }))
    .pipe(slim({pretty: true}))
    .pipe(gulp.dest('dist'));
});
现在可以翻译为:

doctype html
html
  head
    title <%= projectTitle %>
  body
    #content
      <% if isHtml5() { %>
      p Routes look like "example.com/some/route"
      <% } else { %>
      p Routes look like "example.com/#!/some/route"
      <% } %>

    #footer
      | Copyright © <%= year %> <%= author %>
文档可在此处找到:

对于这种特殊情况,另一种选择是gulp jade For jade模板,类似于slim,但您可以使用vanilla jade语法而不使用gulp模板


其他通用模板引擎包括:swig、mustache等。

您指的是这个吗?你能再详细说明一下你是如何使用中间商的,以及你希望gulp在这个系统中扮演什么角色吗?啊,不,我指的是我以前使用的,而不是gulp/grunt来生成资产。这一位是erb sintax,所以基本上我能够从config.rb向任何模板公开一些变量和一些助手:PI最终使用了lodash模板,很快就会发布我问题的答案