Skip to content

Instantly share code, notes, and snippets.

@lunelson
Created November 8, 2013 07:53
Show Gist options
  • Save lunelson/7367645 to your computer and use it in GitHub Desktop.
Save lunelson/7367645 to your computer and use it in GitHub Desktop.
Mixin to Mixin passing of data using 'unique-id()' function
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.10)
// ----
/*
Using the unique-id() function to store temporary data in a global map
as a means to pass information to sub-routine mixins and back again
*/
// utility function for nested map-getting
@function get-n($map, $keys...) {
@each $key in $keys { @if $map == null { @return null; } @else { $map: map-get($map, $key); } }
@return $map; }
// a global map/hash/object to store temporary data
$global: ();
// a sub-mixin which processes input data and pushes it to global under key 'id'
@mixin sub-mix($id, $data) {
$alpha: get-n($data, $id, alpha);
$beta: get-n($data, $id, beta);
$data: map-merge($data, (#{$id}: (
alpha: $alpha * 2,
beta: $beta * 2,
gamma: 20
)));
$global: map-merge($global, $data);
}
// a mixin which sends data to submixin then retrieves result via id from global
@mixin main-mix($alpha, $beta) {
$id: unique-id();
$data: (#{$id}: (
// create whatever here
alpha: $alpha,
beta: $beta
));
@include sub-mix($id, $data);
$data: get-n($global, $id);
debug: inspect($data);
}
.debug {
@include main-mix(2,3);
}
/*
Using the unique-id() function to store temporary data in a global map
as a means to pass information to sub-routine mixins and back again
*/
.debug {
debug: (alpha: 4, beta: 6, gamma: 20);
}
@lunelson
Copy link
Author

lunelson commented Nov 8, 2013

A basic example of using a global map to pass data from a sub-mixin back to a parent mixin. What it lacks is a good function packing individual variables back up in to a map again, which I think they tried to do but failed (see issue: sass/sass#1000). Also it would benefit from recursive merge() function which I tried to write but haven't managed yet. Included at the top however is a recursive get() function

@lunelson
Copy link
Author

lunelson commented Nov 8, 2013

Actually iterative get() function, to be more precise ;)

SM live view here http://sassmeister.com/gist/7367645

@mirisuzanne
Copy link

Interesting. What's the advantage of this approach over passing the map to a function, and returning the altered map?

Also, I like your get-n. I built something similar I was calling sub-get. I'd be really interested in a sub-merge or merge-n, allowing you to easily alter nested maps. Have any ideas on that one? :)

@lunelson
Copy link
Author

lunelson commented Feb 9, 2014

@ericam to follow up on this, I built a set of functions that do nested merging and setting in such a way that they can also transparently replace the native ones (argument parsing handles regular or nested cases). I built them as a recreation of map functionality for libsass using lists, but will also do a ruby sass native-maps version.

https://github.com/lunelson/sass-list-maps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment