How to check express local variable exists when in script tag of jade?


I have a data variable sent to client-side, but it may not always be included as a variable in the express locals. If it doesn't exist, var data = !{JSON.stringify(data)}; returns var data = ; which causes a js error.

I've tried using conditionals prefixed with '-' but that doesn't seem to work.

    - if
        var data = !{JSON.stringify(data)};
    - else
        var data = {};

How do I give it a default if is undefined?

Problem courtesy of: dule


Don't you hate it when you wrack your brain, then ask for help on SO, only to figure it out 5min later...

Looks like the following keeps the jade and javascript happy:

var data = !{ JSON.stringify( || '') };
Solution courtesy of: dule


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.