14

I'm trying to add a dynamic class to a jade template. Like so:

- var obj = {a: 1, b: 2, c: 3};
- var len = Object.keys(obj).length;

.abc-#{len}

But the compiler is taking exception to this:

  > 4| .abc-#{len}
------------^

Unexpected token `interpolation` expected `text`, `interpolated-code`, `code`, `:`, `slash`, `newline` or `eos`

I've tried everything I could think of. Been scouring https://pugjs.org/language/interpolation.html. Could really use a hand.

Thanks.

NotoriousWebmaster
  • 3,298
  • 7
  • 37
  • 49

2 Answers2

27

You can do this:

div(class="abc-"+len)

attributes are interrupted automatically, more about attributes

TedMeftah
  • 1,845
  • 1
  • 21
  • 29
9

You can use ES6 template literals as well. E.g.

div(class=`static_${dynamic_variable}` 

In your case:

div(class=`abc-${len}`)

Have fun.

Sean
  • 6,873
  • 4
  • 21
  • 46
Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56