How do I check if a SASS Map already exists and only define one if this is not the case?
I have tried:
#if ($myMap) {
// do someting
}
and
#if variable-exists($myMap) {
// do something
}
But I get the error "Undefined variable: "$myMap"?
I'm sure this is pretty straghtforward but I can't seem to find the answer online?
Thanks
It's a little confusing, but when checking for a variable's existence, skip the $. You also need to set it as a global variable so it doesn't get scoped only to the #if block. This works:
#if variable-exists(myMap) == false {
$myMap: (
1: "foo",
2: "bar"
) !global;
}
// ... now you can use your variable
You can set the map to exist as null be default, which means that if it isn't explicitly set anywhere, then the variable still exists but with a null value. You can then check if the value of the variable is null using an if statement.
$myMap: null !default;
#if $myMap == null {
}
Related
Here is the application.properties file:
myVar=${SOME_VAR:#{null}}
result=myVar is #{myVar != null && myVar.length() > 0 ? '' : 'not'} populated
What I am trying to get is if the environment variable SOME_VAR is set (and not blank), the property result should be myVar is populated, otherwise myVar is not populated.
The code I put above doesn't work (the line to set result), and I have also tried different combinations of #{} and ${}, including wrapping myVar, but no success so far.
What is the correct way to do? Thanks.
You wont be able to refer the myVar field directly if your member variable are private. So you should put your condition directly on the property value.
please check below expression as per your requirement.
#Value("myVar is #{ '${SOME_VAR}' != null && '${SOME_VAR}'.trim().length() > 0 ? '' : 'not'} populated")
private String result;
The #{ } is an expression language feature, while ${ } is a simple property placeholder syntax.
I ended up doing
result=myVar is #{'${SOME_VAR:#{null}}' != '#{null}' && '${SOME_VAR:#{null}}'.trim().length() > 0 ? '' : 'not'} populated
How I can check existing property $this->team->playerAssignment->player in more rational way? Now I check it like this:
if ($this->team)
if (($this->team->playerAssignment))
if (($this->team->playerAssignment->player))
Try isset php function.
isset — Determine if a variable is set and is not NULL
if(isset($this->team->playerAssignment->player)){
}
The following has always works best for me:
if(isset(Auth::user()->client->id)){
$clientId = Auth::user()->client->id;
}
else{
dump('Nothing there...');
}
The best way is
if (
isset($this->team)
&& isset($this->team->playerAssignment)
&& isset($this->team->playerAssignment->player)
){
// your code here...
}
Because PHP will stops if the first is to false, if first object exists, it continue to second, and third condition...
Why not use only && $this->team->playerAssignment->player ?! because if player has 0 for value it will be understood as a false but variable exists !
You can easily check by null coalescing operator
if ($this->team->playerAssignment->player ?? null)
I need to store 'null' as a string in the .env file.
Now I have VAR_NAME=null in the file and if I try to read the value with
env('VAR_NAME') it doesn´t return nothing.
Is there any way way to store the value as a string?
You can store a blank string in your .env file by leaving the space after = blank, such as `
VAR_NAME=
But it won't return null, it will return an empty string:
var_dump(env("VAR_NAME");
//string(0) ""
If you want it to return NULL, then use
VAR_NAME=NULL
Now, if you want the string "NULL", you would have to have some custom logic to convert it at runtime, for example:
$test = env("VAR_NAME") ? env("VAR_NAME"):"NULL";
var_dump($test);
// string(4) "NULL"
If you put VAR_NAME=null in .env file and try to print it as {{ env('VAR_NAME') }} it wouldn't show anything. But the value is still null.
You can see it by using {{ var_dump(env('VAR_NAME'))}}
But if you want to return null as a string, I don't think it's possible. You would probably need to specify any other string and consider it as null.
If you just wanted to store it in a global variable, you can save it in the config/app.php file and use it as {{Config::get('app.var_name')}} This would return null.
//config/app.php
'var_name' => 'null',
Now access it via:
config('app.var_name')
or
Config::get('app.var_name')
The anser to my question seems to be, no, you can't.
The solution was to use the second parameter of env('VAR_NAME','default').
The problem is that env() doesn't return the default value because VAR_NAME exists and have a value.
I comented VAR_NAME in the env file just in case I need to use it later.
You can use php's getenv function and pass a default value. So if the value is not existing then it will return the default value for eg:
getenv('VAR_NAME',null)
This will return the value if VAR_NAME is existing in the .env file else it will return you the default value which is null
L8 try
env('VAR_NAME') ?: "null";
Cover cases:
if VAR_NAME not exists in .env you will get 'null'
if VAR_NAME= in .env you will get 'null'
I have the following mapping to contain all of the colours from my theme:
$_base-ocean:rgb(13,176,184);
$_base-hover:10%;
$themes: (
ocean: (
base: $_base-ocean,
hover: darken($_base-ocean, $_base-hover)
)
);
I know how to use an #each loop to get the key/value information from a mapping, but how can I directly access the value of a mapping without using a loop? I tried using square brackets like you would in other languages like JavaScript:
#each $name, $colors in $themes {
[data-page="home"] {
#slider-pagers{
a.#{$name} {
background-color: $colors[base]; // <- line 21
}
}
}
}
But I get a syntax error instead:
error sass/test.scss (Line 21: Invalid CSS after "...d-color: $color": expected ";", was "[base];")
You have the use the map-get function. Sass does not provide a special syntax for accessing values of a mapping.
#each $name, $colors in $themes {
[data-page="home"] {
#slider-pagers{
a.#{$name} {
background-color: map-get($colors, base);
}
}
}
}
Now you get the following output:
[data-page="home"] #slider-pagers a.ocean {
background-color: #0db0b8;
}
A good practice when using SassScript maps (not "source maps"; those are different) is to always quote the keys. For example:
$site-global: (
"ocean": (
"base": $_base-ocean,
"hover": darken($_base-ocean, $_base-hover)
)
);
In order to be compatible with CSS, Sass interprets some unquoted identifiers (including ocean) as color names and translates them internally to color values. When emitting compressed output, Sass will try to produce the smallest possible representation of those colors, which in this case is a hex code. Quoting the keys makes it clear that they should always be strings and should always be emitted with their string values.
Fixed Issue 1 !
It was a case of doing another map-get within a map-get. Once done, I had to reference the default value (this being base). As long as all my default values had a base value this seems to work fine:
#each $theme-colour, $color in $site-global {
[data-page="home"]{
#slider-pagers a.#{$theme-colour}{
background-color:map-get(map-get($site-global, $theme-colour), base);
}
}
}
How it still fails to minify when this code:
#{$variable_here}
Funny enough this code does not:
#slider-pagers a #{$theme-colour}{
or
#slider-pagers a#{$theme-colour}{
Thanks for responding, if anyone knows the is compiling issue that would be great.
AS the title says I am trying to check whether a variable is defined in SASS. (I am using compass if that makes any different difference)
I've found the Ruby equivalent which is:
defined? foo
Gave that a shot in the dark but it just gave me the error:
defined": expected "{", was "?
I've found a work around (which is obviously just to define the variable in all cases, which in this case it actually makes more sense) but I'd really like to know if this is possible for the future
For Sass 3.3 and later
As of Sass 3.3 there is a variable-exists() function. From the changelog:
It is now possible to determine the existence of different Sass constructs using these new functions:
variable-exists($name) checks if a variable resolves in the current scope.
global-variable-exists($name) checks if a global variable of the given name exists.
...
Example usage:
$some_variable: 5;
#if variable-exists(some_variable) {
/* I get output to the CSS file */
}
#if variable-exists(nonexistent_variable) {
/* But I don't */
}
For Sass 3.2.x and earlier (my original answer)
I ran into the same problem today: trying to check if a variable is set, and if so adding a style, using a mixin, etc.
After reading that an isset() function isn't going to be added to sass, I found a simple workaround using the !default keyword:
#mixin my_mixin() {
// Set the variable to false only if it's not already set.
$base-color: false !default;
// Check the guaranteed-existing variable. If it didn't exist
// before calling this mixin/function/whatever, this will
// return false.
#if $base-color {
color: $base-color;
}
}
If false is a valid value for your variable, you can use:
#mixin my_mixin() {
$base-color: null !default;
#if $base-color != null {
color: $base-color;
}
}
If you're looking for the inverse, it's
#if not variable-exists(varname)
To check if it is undefined or falsy:
#if not variable-exists(varname) or not $varname
And if you want to set it only if it's undefined or null
$varname: VALUE !default;
Just as a complementary answer - you should have a look on the default keyword for certain use cases. It gives you the possibility to assign a default value to variables in case they are not defined yet.
You can assign to variables if they aren’t already assigned by adding
the !default flag to the end of the value. This means that if the
variable has already been assigned to, it won’t be re-assigned, but if
it doesn’t have a value yet, it will be given one.
Example:
In specific-variables.scss you have:
$brand: "My Awesome Brand";
In default-variables.scss you have:
$brand: company-name !default;
$brand-color: #0074BE !default;
Your project is built like this:
#import "specific-variables.scss";
#import "default-variables.scss";
#import "style.scss";
The value of brand will be My Awesome Brand and the value of brand color will be #0074BE.