assigning value to a variable inside a jade file

pug
Erel Segal-Halevi picture Erel Segal-Halevi · Dec 23, 2012 · Viewed 17.5k times · Source

Is it possible to assign variables inside a jade file, in order to make the code more readable?

Specifically, I created this jade file:

extends layout
- var format = "%+1.0f"
block title
  title Your score table
block body
 ...
      td.utilityUtil #{sprintf(format,value)}
 ...

And got an error in the last line, that "format" is not defined. Probably I don't assign it correctly, but I didn't find the correct syntax.

P.S. I have Express 3.

Answer

Guy picture Guy · Jan 4, 2013

You have to declare the variable in the block scope :

extends layout
block title
title Your score table
block body
    - var format = "%+1.0f"
    ...
    td.utilityUtil #{sprintf(format,value)}
    ...