Java Less4j,无法将自定义函数用作路径变量
我正在尝试使用less4j中的自定义函数,这些函数返回了在css中使用的公共路径:Java Less4j,无法将自定义函数用作路径变量,java,less,Java,Less,我正在尝试使用less4j中的自定义函数,这些函数返回了在css中使用的公共路径: com.github.sommeri.less4j.Less4jException: Could not compile less. ERROR style.less 94:27 Unable to evaluate expression details summary { 94: background-image: url(common()/img/ic_g_down.png); 95:
com.github.sommeri.less4j.Less4jException: Could not compile less.
ERROR style.less 94:27 Unable to evaluate expression
details summary {
94: background-image: url(common()/img/ic_g_down.png);
95: background-position: left center;
功能是:
public class ConstantLessFunction implements LessFunction {
private static String fs[] = {
"common",
...
};
@Override
public boolean canEvaluate(
FunctionExpression call, List<Expression> parameters) {
return ArrayUtils.contains(fs, call.getName());
}
@Override
public Expression evaluate(
FunctionExpression call, List<Expression> parameters,
Expression evaluatedParameter, LessProblems problems) {
String name = call.getName();
switch (name) {
case "common":
return new IdentifierExpression(call.getUnderlyingStructure(), "/common/images/");
break;
...
}
}
公共类ConstantLessFunction实现LessFunction{
专用静态字符串fs[]={
“普通”,
...
};
@凌驾
公共布尔值(
函数表达式调用,列出参数){
返回ArrayUtils.contains(fs,call.getName());
}
@凌驾
公众表达评价(
函数表达式调用,列出参数,
表达式求值参数,LESSP问题){
String name=call.getName();
交换机(名称){
案例“普通”:
返回新的IdentifierExpression(call.getUnderlineStructure(),“/common/images/”;
打破
...
}
}
我之所以使用IdentifierExpression,是因为wiki上的示例
也许还有另一种方法可以做到这一点,比如在java中向编译器添加变量或者类似的东西
谢谢,Phaedra。您不能使用这样的函数(无论是内置函数还是自定义函数,即函数调用不是字符串,因此您不能将其结果与另一个字符串(如
foo()bar
)连接起来)。对于您的示例,您首先需要将函数调用结果分配给一个变量,然后才将该变量与字符串的其余部分连接起来,例如@some path:common()
然后背景图像:url(“@{some path}/img/ic_g_down.png”)
(或背景图像:url(@{some path}/img/ic_g_down.png)
取决于less4j是否支持语法)。也许更好的方法是在编译源代码时通过java将变量注入到less4j上下文中,这可能吗?不,我不知道(至少我在less4j
中看不到类似--modifyVars
的内容)。或者,您可以向common
函数添加一个参数,以便像背景图像:url(common(img/ic_g___down.png));
一样使用它(尽管可能还需要将url
部分移动到函数中)。另一种方法是使用函数,例如背景图像:%(~'url(%a/img/ic_g_down.png)”,common();
等等。最后,我决定在我的css开始时使用@common:common();和so background image:url(“@{common}/”);谢谢!