Sass control directive with decimal numbers - sass

I'm trying to get a sass function going:
$times: 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.75 0.80 0.90;
#each $value in $times{
.is-hidden-#{$value}{
#include hide( $value + s );
}
}
The problem is this: somehow, it doesn't accept decimal numbers. It works perfectly with 1, 2, 3, etc. I even tried this:
$times: 10 20 30 40 50 60 70 80 90;
#each $value in $times{
.is-hidden-#{$value}{
#include hide( ($value / 100) + s );
}
}
But sass doesn't do anything with that last calculation there. He just ignores the '/ 100'. Is there some way I can pass decimal numbers through a function like this? The output would be this:
.is-hidden-0.1s{
}
.is-hidden-0.2s{
}
.is-hidden-0.3s{
}
etc....
Thanks!
--- EDIT ---
This is the hide mixin:
#mixin hide( $time ){
opacity: 0;
visibility: hidden;
#include transition( $time );
}

Your desired output is invalid CSS. From the CSS validator:
In CSS1, a class name could start with a digit (".55ft"), unless it was a dimension (".55in"). In CSS2, such classes are parsed as unknown dimensions (to allow for future additions of new units) To make "1s" a valid class, CSS2 requires the first digit to be escaped ".\31s" [1s]
There are many times where Sass will give you errors when you try to generate invalid CSS. To generate valid results, you need to compose the selector using integers and adding the escaped decimal point like this:
#for $i from 1 through 9 {
.is-hidden-0\.#{$i}s {
#include hide($i / 10 * 1s);
}
}

Here's a general way to escape decimal numbers for class names (replacing . with -)
#function strip-units ( $number ) {
#return $number / ( $number * 0 + 1 );
}
#function escape-number ( $value ) {
#if type-of( $value ) != 'number' {
#return $value;
}
$int: floor( strip-units( $value ) );
$fract: $value - $int;
#if ( $fract == 0 ) {
#return $int;
}
#while ( $fract != floor( $fract ) ) {
$fract: $fract * 10;
}
#return $int + '-' + $fract;
}

Related

SCSS - Loop while adding text

In my SCSS file I have this:
#for $i from 1 to 100 {
$percent : $i + '%';
[fxFlex='$percent'] {
flex: 1 1 $percent ;
}
}
I want 1%, then 2% etc but I get this. I wish to remove the double quotes and have $percent display as 1%, 2% not $percent:
[fxFlex=' $percent '] {
flex: 1 1 "1%";
}
[fxFlex=' $percent '] {
flex: 1 1 "2%";
}
You're almost there.
You need to use variable interpolation, meaning you have to write #{$percent} instead of $percent:
Interpolation can be used almost anywhere in a Sass stylesheet to embed the result of a SassScript expression into a chunk of CSS. Just wrap an expression in #{} in any of the following places:
Source: sass-lang
Second thing, you probably want to remove the quotes in the flex property. You can do it with the string function string.unquote(), but first you need to import it with the use keyword.
#use "sass:string";
#for $i from 1 to 100 {
$percent: $i + '%';
[fxFlex='#{$percent}'] {
flex: 1 1 string.unquote($percent);
}
}

How to check if number is a decimal and run function laravel

I use Ajax to search my data and when it runs it removes price formatting from 49.99 to 4999. I can solve the problem by using ${{number_format($product->price / 100, 2)}} in the view file but the problem is if the price is displaying correctly as 49.99 and the function runs then it now displays as 0.50. How can I run a check so that this function only attempts to format price if it doesn't have decimal, only if its a int like 4999
I tried
if(is_float($product->price)){
echo $product->price;
} else {
echo number_format($product->price / 100, 2);
}
This worked for me
if (is_numeric($product->price) && floor($product->price) != $product->price) {
echo $product->price;
} else {
echo number_format($product->price / 100, 2);
}

In SASS how do you return a list from a function?

I am working on an angular library and am trying to create a function that takes a color pallet and a name and returns a list so I can create CSS variables. Any help or ideas is appreciated!
$violets-palette: (
100: #e1cced,
200: #9064b3,
300: #6a408a,
400: #5d357e,
500: $violet,
600: #311a52,
700: #2d054c,
800: #28053d,
900: #1f0532
);
$reds-palette: (
50: #fee9e8,
100: #fbc8c6,
200: #f9a4a1,
300: #f77f7b,
400: #f5635e,
500: $precise-red,
600: #f1413c,
700: #ef3833,
800: #ed302b,
900: #ea211d
);
#function createVariables($palette, $name){
#each $key, $value in $palette {
#if $key != 'contrast' {
#if $key == 500 {
#return '--' + $name + ':' + #{$value};
} #else {
#return #{'--' + $name + '-' + $key} + ':' + #{$value};
}
}
}
}
:root{
//This would loop over the returned list
createVariables($reds-palette, 'precise-red');
}
I want it to add this to the style sheet...
--precise-red-50
--precise-red-100
--precise-red-200
--precise-red-300
--precise-red-400
--precise-red
--precise-red-600
--precise-red-700
--precise-red-800
--precise-red-900
Why your #function not works: With #return you end the function and #return's the result ... so your #function will stop on the first step in your loop.
So, if you really want to use a function first write the values to a map and return the map to a var.
But note: in SASS a #function is not the best choice to write code to css. It is more used to return a single value. If you would need a multiused solution use a #mixin (it is to write calculated code to the css file) or if it is a single task in your project a simple #each loop does the job as well.
One more thinking:
Only writing var names to css does not even helps without advising a value to it. So maybe you like to do:
:root {
--precise-red-50: #fee9e8;
--precise-red-100: #fbc8c6;
...
}
However. We had a very similar question here some days ago. Maybe you like to have a look ... It was about writing css color vars based on lists/maps similar to yours to css file. If with or without advising color to the css vars ... please adapt the code to your needs:
https://stackoverflow.com/a/66854088/9268485
Update: Additional Example
Here is another similar example from the last days. In this case the task was to write the color swatch build on a color direct to css vars. The shades had been calculated in same process:
https://stackoverflow.com/a/66850423/9268485
(If you are not confirm with Bootstrap colors and color maps here is a short overview to this example: https://getbootstrap.com/docs/5.0/customize/color/)

SCSS function remains uncompiled in CSS output

Seems like I must have some syntax error with my function?
Defined as so:
#function v($i, $a: $ba) {
$ca: $i*$a;
$x: $r*(sin($ca) + $p*sin($q*$ca));
$y: $r*(cos($ca) - $p*cos($q*$ca));
$z: $r*sin(($q + 1)*$ca);
#return $x, $y, $z;
}
in my output style.css document I still see the function calls:
--v: v($i)
In this case, you need to use interpolation where you call your function:
.selector {
--v: #{v($i)};
}

Smarty Date Difference

Please post any smarty syntax to Get number of days between two dates from my database. i was to able to display all the other fields,but this date field with number of days not working as the way i was expected.Please let me know is there any way to get this solution without any smarty additional plugins.
Smarty does not include any specific functions for doing date math operations. They have the date_format for timestamps, but otherwise you'd either have to write your own days_diff plugin, find one online, or do the date math in PHP and assign to a new variable in Smarty.
Here is my function for this problem:
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: date_diff
* Author: RafaƂ Pawlukiewicz
* Purpose: factor difference between two dates in days, weeks, or years
* Input: d1 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd"
* d2 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd" or $smarty.now
* assign = name of variable to assign difference to
* interval = "days" (default), "weeks", "years"
* Examples: {date_diff d1="2020-01-20"}
* Examples: {date_diff d1="2020-01-20" d2=2020-02-10 interval="weeks"}
* Examples: {date_diff d1="2020-01-20" d2=2020-02-10 assign="variable_diff"} result: {$variable_diff}
* -------------------------------------------------------------
*/
function smarty_function_date_diff($params, &$smarty)
{
$d1 = isset($params['d1']) ? $params['d1'] : date('Y-m-d');
$d2 = isset($params['d2']) ? $params['d2'] : date('Y-m-d');
$assign_name = isset($params['assign']) ? $params['assign'] : '';
$date1 = strtotime($d1);
$date2 = strtotime($d2);
// use current for empty string
if (! $date1) {
$date1 = date('Y-m-d');
}
if (! $date2) {
$date2 = date('Y-m-d');
}
$interval = isset($params['interval']) ? $params['interval'] : 'days';
// diff in days
$diff = ($date2 - $date1) / 60 / 60 / 24;
if ($interval === "weeks") {
$diff /= 7;
}
elseif ($interval === "years") {
$diff /= 365.25;
}
$diff = floor($diff);
if ($assign_name) {
$smarty->assign($assign_name, $diff);
}
else {
return $diff;
}
}

Resources