Can Sass evaluate strings that contain mathematica

2020-02-13 04:44发布

问题:

I'm trying to create a function which will output a list of numbers based on an expression given to it.

Does anyone know how I can pass an expression through a function and have it evaluated within the function?

This is what I have so far:

@function patt($expression, $b: 10) {
    $result: ();
    @for $i from 1 through 10 {
        $result: append($result, unquote($expression));
    }
    @return $result;
}

Example usage:

$list: patt('$i * $b + 2');

Unfortunately this doesn't work. Presumably the expression is being treated as a string within the function.

回答1:

No. Sass does not have an eval function. The closest you can get is by using the call function.

@function my-expression($x, $y) {
  @return $x * $y + 2;
}

@function patt($expression, $b: 10) {
    $result: ();
    @for $i from 1 through 10 {
        $result: append($result, call($expression, $i, $b));
    }
    @return $result;
}

$list: patt('my-expression'); // 12 22 32 42 52 62 72 82 92 102


标签: sass