1

我正在使用 Symfony 1.4,并且在使用 LESS CSS 预处理器时遇到了一些问题。

假设我有 2 个带有颜色特定变量的 Less 文件。它们被称为blue.lessred.less

他们来了:

无蓝

@mainBorder: blue;
@pulldownBackground: blue;

红色.less

@mainBorder: red;
@pulldownBackground: red;

现在假设我有一个layout.less文件,看起来像这样:

// Colored line under Nav
.main {
    border: 1px solid @mainBorder;
    .pullDown { background: @pullDownBackground; }
}

如果我想使用其中一个颜色变量文件,我需要在layout.less文件的顶部声明它,如下所示:

@import 'red.less';

由于该语句必须引用特定文件,所以每当我想更改配色方案时,我@import如何能够动态传递给 @import 语句?blue.less

有没有办法使用 PHP 和 Symfony 框架动态声明哪些颜色特定的 LESS 文件将传递给该导入语句?

或者这个问题可以在没有服务器端代码的情况下解决吗?

4

1 回答 1

1

我从未使用过 Symphony,但这应该让您朝着正确的方向前进,而不管框架如何。

首先,您要创建 LESS 文件:

<?php
$color_scheme = "red"; // we're simplifying here for now, but this could be set via $_POST variable

/*
    it would probably be a good idea to check if the file exists first before we import it.
    see function: file_exists()
*/

$contents = "
@import '$color_scheme.less';
@import 'main_styles.less';
@import 'other_stuff.less';
";

file_put_contents("path/to/styles.less");
?>

现在您有一个准备好处理的 LESS 文件,就像您手动编写它一样,但颜色方案很容易交换。如果我使用纯 PHP 执行此操作,我将使用该exec()函数来调用可通过命令行使用的命令。这是通过调用 SASS 的示例exec()(我从未以这种方式使用过 LESS,因此您必须在此处填写空白)。

<?php
exec("sass --compile path/to/sass:path/to/public/css");
?>

如果您的 Symphony 插件无需使用 exec/命令行即可为您进行编译,那么您可能希望这样做。

于 2012-12-04T15:52:50.673 回答