1

本主题的答案中,展示了如何使用 BundleTransformer for LESS 和 LessTranslator 将变量传递给 LESS 文件。不幸的是,在 SASS 类型的 BundleTransformer 中,GlobalVariables 属性不可用。是否可以使用自定义变量(颜色值)编译 SASS 文件取决于登录用户?

4

2 回答 2

1

我没有使用过 SASS 扩展,所以我不能肯定地说,但你总是可以创建一个自定义包转换来替换本质上是一个“模板”文件。

所以你可以有一个带有像这样声明的变量的 SASS 文件

$variable: {{variable-placeholder}};

然后使用类似于以下内容的内容来替换这些值。

public class ReplacePlaceholdersBundleTransform : IBundleTransform
{
    private readonly IDictionary<string, string> _replacements;

    public ReplacePlaceholdersBundleTransform()
    {
        _replacements = new Dictionary<string, string>();
    }

    public ReplacePlaceholdersBundleTransform(IDictionary<string,string> replacements)
    {
        _replacements = replacements ?? new Dictionary<string,string>();
    }

    public void Process(BundleContext context, BundleResponse response)
    {
        if (_replacements.IsNullOrEmpty())
            return;

        foreach (var replacement in _replacements)
        {
            response.Content = response.Content.Replace(replacement.Key, replacement.Value);
        }
    }
}

要使用它,请将转换添加到包中。

yourBundle.Transforms.Add(
            new ReplacePlaceholdersBundleTransform(new Dictionary<string, Func<BundleContext, string>>
            {
                {"{{variable-placeholder}}", "red"},
            }));

我们在脚本转换中使用了类似的东西,在运行时将捆绑 URL 注入脚本文件,以获取优化框架生成的完整 URL。诚然,如果在 SASS 翻译发生后执行转换,这可能对您不起作用,但是,我没有时间启动一个 SASS 项目来测试它。

我希望这有帮助。

于 2015-06-02T20:38:59.443 回答
1
于 2017-05-10T17:12:51.993 回答