【问题标题】:Assigning a data parameter to a Jade mixin将数据参数分配给 Jade mixin
【发布时间】:2015-09-29 01:31:18
【问题描述】:

这是我在 mixins.jade 中的 mixin

mixin link(url, name)
  a(href='#{data.url}', title=name)&attributes(attributes)= name

我在 data.json 中的数据

{
    "facebook": "fb.com",
    "twitter": "tt.com",
}

这是模板中调用的mixin

+link('facebook', 'Go to facebook')(class="social-link")

gulpfile.js 中的 gulp 处理

gulp.task('templates', function() {

  gulp.src(srcDir + '/html/template.jade')
    .pipe(jade({
      locals: {
        data: data,
        pretty: true
      }
    }))
    .pipe(rename('intermediate.html'))
    .pipe(gulp.dest(srcDir + '/html'))
});

最终结果给出未定义的值

<a href="undefined" title="Go to facebook" class="social-link">Go to facebook</a>

目前还没有办法在 mixin 中调用我的数据。我知道数据正在工作,因为如果我在 mixin(例如)中提供 data.facebook 而不是 data.url,那么我将呈现正确的 url。

也不起作用: 混合链接(网址,名称) a(href='#{data.'+url+'}', title=name)&attributes(attributes)= name

它呈现

<a href="#{data.webversion}">

而不是&lt;a href="fb.com"&gt;

【问题讨论】:

    标签: javascript node.js gulp pug templating


    【解决方案1】:

    如果你使用会怎样

    mixin link(url, name)
      a(href=url, title=name)&attributes(attributes)= name
    

    编辑:或者也许

    mixin link(url, name)
      a(href='#{data[url]}', title=name)&attributes(attributes)= name
    

    【讨论】:

    • 如果我这样做,我的模板将如下所示+link('#{data.facebook}', 'Go to facebook')(class="social-link") 这对我来说太多了
    • 将尝试您的第二个建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多