Q:

Pug/Jade and inline javascript calculations

I'm lost, again, trying to do some simple calculations in jade template.
Given this data object:
{
  "trade": {
    "name": "Mogens",
    "dst_currency": "EUR",
    "dst_value": 115.7,
    "src_price": null,
    "src_value": 2,
    "src_currency": "XMR",
    "date": null
    }
}

And this pug source:
table
  thead
    tr
      th Currency
      th Quantity
      th Price
      th Total
      th Date
  tbody
      tr
        script.
          if (trade.dst_currency === "EUR")
            trade.src_price = trade.dst_value / trade.src_value
          else
            trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
        th.align-middle #{trade.dst_currency}
        th.align-middle #{trade.dst_value}
        th.align-middle= #{trade.src_price}
      th.align-middle #{trade.src_value} #{trade.src_currency}
      th.align-middle #{trade.date}

if trade.name === "Bob"
  h1 Hello Bob
else
  h1 My name is #{trade.name}

How is this done, if at all possible? What am I missing?

A:

Placing a script tag in your Pug code renders a script tag in your compiled HTML. It does not tell Pug to execute any javascript within the script tag when it compiles. If you want to run javascript in Pug as your code is compiled, use an unbuffered code block.
-
  // this is an unbuffered code block
  // that will update the value of `trade.src_price`
  // before it is rendered by Pug
  if (trade.dst_currency === "EUR") {
    trade.src_price = trade.dst_value / trade.src_value
  } else {
    trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
  }

table
  thead
    tr
      th Currency
      th Quantity
      th Price
      th Total
      th Date
  tbody
    tr
      th.align-middle #{trade.dst_currency}
      th.align-middle #{trade.dst_value}
      th.align-middle= #{trade.src_price}
      th.align-middle #{trade.src_value} #{trade.src_currency}
      th.align-middle #{trade.date}

if trade.name === "Bob"
  h1 Hello Bob
else
  h1 My name is #{trade.name}

