翡翠混合属性未应用

Jade mixin attributes not applied

本文关键字:应用 属性 混合      更新时间:2023-09-26

我正在使用 jade 来模板化报告表,如下所示:

mixin report_row(row)
  tr(data-id=row.c[keyIndex].v)
    each cell, i in row.c
      +cell_decorator(data.cols[i], cell)(data-foo="bar")

它是由修饰的报表单元格组成的报表行的嵌套混合结构。

问题是没有应用 data-foo 属性。

我已经在 SO 上看到了有关 mixin 属性的其他问题,但我找不到模板的任何语法问题,它只是在忽略属性的同时呈现。

该文档显示了将属性传递给mixins的示例:

mixin link(href, name)
  //- attributes == {class: "btn"}
  a(class!=attributes.class, href=href)= name
+link('/foo', 'foo')(class="btn")

请注意,mixin 本身使用隐式attributes名称来引用传递的属性 - 换句话说,属性不会自动应用,它们只是作为参数发送到 mixin 中。您必须更改cell_decorator mixin 的定义以考虑属性。

如果你想简单地在mixin上应用属性,你可以使用&attributes语法:

mixin cell_decorator(colname, data)
    //- the `attributes` get applied to the td
    td(...)&attributes(attributes)
+cell_decorator(data.cols[i], cell)(data-foo="bar")

请注意,使用这样的&attributes(带有 mixin 调用)是安全的,因为这些值在调用期间会被转义。