Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less if string is not empty conditional logic

Tags:

css

less

dotless

I'm using dotless to dynamically change the look of my site from an admin page.

Essentially I use regular expressions to read the variables defined in the less and then give the user the option to change the value of the variables.

I'm wanting to have the option to set a background image. Essentially I need a way to check if the string is empty if its not then add the background image mixin.

@BackgroundImage: '';

.showBackground(@fileName) when (@fileName != '') {
    background-image: url('../Themes/images/backgrounds/@{fileName}');
}

body {
    .showBackground(@BackgroundImage)
}

So the default is no background '' when the user sets a background the variable @BackgroundImage will be set to 'backgroundImage1.jpg'

How can I get this empty string logic to work?

P.S I've tried setting the variable to #000000 and using isstring() but it would appear to return true.

like image 222
John Avatar asked Sep 28 '26 15:09

John


1 Answers

You want to use the when not instead of negating the condition.

LESS

.showBackground(@fileName) when not (@fileName = '') {
    background-image: url('../Themes/images/backgrounds/@{fileName}');
}

Output

@BackgroundImage: ''; // No output.
@BackgroundImage: 'foo'; // background-image: url('../Themes/images/backgrounds/foo');
like image 157
thgaskell Avatar answered Sep 30 '26 03:09

thgaskell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!