Pass list of functions as mapping value in scss - sass

How can I pass a list of functions as the value of a mapping in SCSS and then run those functions?
This is what I have
$mappings: (
'x':(func1('1', '2'), func2('1', '2', '3'))
);
#include apply-mappings($mappings);
// In my mixin file
#mixin apply-mappings($mappings) {
#each $key, $value in $mappings {
#if type-of($value) == 'list' {
#each $item in $value {
// Run $item which is a function
}
}
}
}
#mixin/function func1($item1, $item2) {
// Do something
}
#mixin/function func2($item1, $item2, $item3) {
// Do something else
}

Related

How to get all routes within a route group?

I need to print a main menu and instead of having a database where the links/ routes are stored I thought it there would be a way to get all routes that are in a named group, but all I find is getting routes by action.
web.php
Route::group(['as' => 'main'], function () {
Route::get('/', function () {
return view('pages.start');
})->name('Home');
Route::get('/foobar', function () {
return view('pages.foobar');
})->name('Home');
Route::get('/business', function () {
return view('pages.business');
})->name('Business');
});
I was looking for something like:
$routes = getRoutesByGroup('main');
I cannot really believe that a function like that doesnt exist in current Laravel but I cant seem to find this. What am I missing?
Maybe this can solve partially in your case
function getRoutesByStarting($start = '')
{
$list = \Route::getRoutes()->getRoutesByName();
if (empty($start)) {
return $list;
}
$routes = [];
foreach ($list as $name => $route) {
if (\Illuminate\Support\Str::startsWith($name, $start)) {
$routes[$name] = $route;
}
}
return $routes;
}
usage
getRoutesByStarting('main')
More general solution
function getRoutesByGroup(array $group = [])
{
$list = \Route::getRoutes()->getRoutes();
if (empty($group)) {
return $list;
}
$routes = [];
foreach ($list as $route) {
$action = $route->getAction();
foreach ($group as $key => $value) {
if (empty($action[$key])) {
continue;
}
$actionValues = Arr::wrap($action[$key]);
$values = Arr::wrap($value);
foreach ($values as $single) {
foreach ($actionValues as $actionValue) {
if (Str::is($single, $actionValue)) {
$routes[] = $route;
} elseif($actionValue == $single) {
$routes[] = $route;
}
}
}
}
}
return $routes;
}
usage
getRoutesByGroup(['middleware' => 'api']);
getRoutesByGroup(['middleware' => ['api']]);
getRoutesByGroup(['as' => 'api']);
getRoutesByGroup(['as' => 'api*']);
getRoutesByGroup(['as' => ['api*', 'main']]);
$allRoutes = Route::getRoutes()->getRoutes(); // fetch all rotues as array
$name = 'main'; // specify your full route name
$grouped_routes = array_filter($allRoutes, function($route) use ($name) {
$action = $route->getAction(); // getting route action
if (isset($action['as'])) {
if (is_array($action['as'])) {
return in_array($name, $action['as']);
} else {
return $action['as'] == $name;
}
}
return false;
});
// output of route objects in the 'main' group
dd($grouped_routes);

How to fetch a value from a map?

How to fetch 'theme' from the below $themes map?
$themes: (
default: (
theme: blue,
)
);
I tried with the below map with single child and it works with map-get()
SCSS
$default: (
theme: red,
);
#function fetchColor($issuer, $key) {
#return map-get($issuer, $key);
}
.navbar-brand {
background : fetchColor($default, theme);
}
Result
.navbar-brand {
background: red;
}
How to fetch the same from '$themes' map?
You can use a solution as described by css-tricks.com:
$themes: (
default: (
theme: blue,
)
);
#function fetchColor($map, $keys...) {
#each $key in $keys {
$map: map-get($map, $key);
}
#return $map;
}
.navbar-brand {
background: fetchColor($themes, default, theme);
}

Laravel cant flatten array after collection->forget

I have a loop inside loop on Laravel collection, and sometimes i need to delete some objects from second loop collection. Here is the code
public function remove_if_found($id)
{
$all_groups = Group::all();
$all_groups->load('templates');
foreach ($all_groups as $group)
{
foreach ($group->templates as $key => $template)
{
if($template->id = $id)
{
$group->templates->forget($key);
}
}
}
return $all_groups;
}
The problem is that collection of group->templates turns from simple(not assoc) array to object. Here is example how response looks
I am trying to flatten $group->templates->flatten() but in final response templates are still as object but not as array.
This test flatten works
...
foreach ($all_groups as $group)
{
foreach ($group->templates as $key => $template)
{
if($template->id = $id)
{
$group->templates->forget($key);
}
}
return $group->templates->flatten()//This code works i get fluttened array
}
But final variant still returns me object instead of array
$all_groups = Group::all();
$all_groups->load('templates');
foreach ($all_groups as $group)
{
foreach ($group->templates as $key => $template)
{
if($template->id = $id)
{
$group->templates->forget($key);
}
}
$group->templates->flatten()//Use flatten here
}
return $all_groups;//Templates are returned not as an array but still as an object (Same variant as on attached image)
}
Use values() to reset the keys and setRelation() to replace the relationship:
public function remove_if_found($id)
{
$all_groups = Group::all();
$all_groups->load('templates');
foreach ($all_groups as $group)
{
foreach ($group->templates as $key => $template)
{
if($template->id = $id)
{
$group->setRelation('templates', $group->templates->forget($key)->values());
}
}
}
return $all_groups;
}
You can also use except() instead of forget():
public function remove_if_found($id)
{
$all_groups = Group::all();
$all_groups->load('templates');
foreach ($all_groups as $group)
{
$group->setRelation('templates', $group->templates->except($id));
}
return $all_groups;
}

How to use this keyword for store variable

How to use this keyword in codeigniter to store values. Beacuse i use same value multiple time in the same page.
So if the this keyword is help to solve this problem.
function1() {
$value = '123456';
$assign1 = $value;
.....
}
function2() {
$value = '123456';
$assign2 = $value;
.....
}
Yes. We store value using this keyword inside the _construct() function to over come this problem.
The sample code,
public function __construct() {
parent::__construct ();
$this->value = '123456';
}
function1() {
$assign1 = $this->value;
.....
}
function2() {
$assign2 = $this->value;
.....
}
You can declare it as a global variable and use with $this, you can do this in a constructor or above constructor, both will work nice
class MyClass extends CI_Controller {
var $value = '123456';//global variable
function __construct() {
parent::__construct();
//$this->value = '123456';
}
function1() {
$assign1 = $this->value;
.....
}
function2() {
$assign2 = $this->value;
.....
}
}

how to get value in 2D or 3D map in SCSS

I know can get value in 1D map with map-get() function but dont know how get value like 'header' in this map:
$mobile-layout: (
layout-values: (
header: (
height: 72px
),
sidebar: (
width: 100%
)
)
);
I have a couple nested loop solutions...
DEMO
3 loops
#each $key, $val in $mobile-layout {
#each $k, $v in $val {
#each $l, $t in $v {
.#{$k} {
.#{$l}: #{$t};
}
}
}
}
2 loops
#each $key, $val in $mobile-layout {
#each $k, $v in $val {
.#{$k} {
.#{map-keys($v)}: #{map-values($v)};
}
}
}

Resources