Changing a variable inside a if - jade/pugjs

2.9k Views Asked by At

I'm trying to make a div have a location inside of it based on the state of two variables, for example if $city = 'New York' and $state = 'NY' the div would display New York, NY. However, it's not working and I think it has to do with scoping. Here's my jade:

$city = data.City
$state = data.State
$locString = ""

if $city != ""
    $locString = $locString + $city
if $city != "" && $state != ""
    $locString = $locString + ", "
if $state != ""
    $locString = $locString + $state

.location #{$locString}

This is ending with $locString being empty, but if I print off $locString inside the if statements it has only the most recently added value, suggesting that it is redeclaring a local copy of the variable inside of each if statement. I can't find anything about this in the reference http://jade-lang.com/reference/

1

There are 1 best solutions below

0
On

I woud suggest this:

- var city = data.City
- var state = data.State
- var locString = ""

if city != ""
    - locString = locString + city
if city != "" && state != ""
    - locString = locString + ", "
if state != ""
    - locString = locString + state

Take care of the "var" and "-".

Check "Variables" part on -> https://naltatis.github.io/jade-syntax-docs/