Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache Velocity:转义字符作为关联数组键不适用于PHP_Java_Php_Templates_Velocity - Fatal编程技术网

Java Apache Velocity:转义字符作为关联数组键不适用于PHP

Java Apache Velocity:转义字符作为关联数组键不适用于PHP,java,php,templates,velocity,Java,Php,Templates,Velocity,我正在使用Velocity(1.7)生成PHP代码 我通过严格的excape设置了速度,这有点帮助: p.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE, "true"); 但我在模板中遇到了以下行的问题: \$result['${attribute.Name}'] = \$this->${attribute.Name}; 输出为: $result['${attribute.Name}'] = $this-&g

我正在使用Velocity(1.7)生成PHP代码

我通过严格的excape设置了速度,这有点帮助:

p.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE, "true");
但我在模板中遇到了以下行的问题:

\$result['${attribute.Name}'] = \$this->${attribute.Name};
输出为:

$result['${attribute.Name}'] = $this->color;
但它应该是:

$result['color'] = $this->color;
编辑:

我找到了解决问题的方法:

#set($d = "$")
${d}result['${attribute.Name}'] = \$this->${attribute.Name};

但我不喜欢只为了解决Velocity中可能存在的错误而引入额外变量的解决方案。

在Velocity 1.7中,
\
不再是转义字符。在呈现
\$result['${attribute.Name}']
时,会逐字打印
\
,因为它不是Velocity语法字符,
$result['${attribute.Name}']
被识别为对
$result
变量引用的对象的数组访问,但由于该变量后面没有数组,整个输出是逐字打印的。Velocity就是这样工作的:任何被认为是有效的Velocity语法,但无法计算或导致
null
,都会被打印回输出

由于混合了两种相似的语法,Velocity和PHP,因此需要一种方法来区分这两种语法。逃逸是正确的选择,但不是。您必须使用一个变量来获取
$
符号

推荐的方法是使用escape工具,但这需要配置工具,并且比只使用
${d}

也可以使用转义语法:

#[[$result[']]#${attribute.Name}#[['] = $this->]]#$attribute.Name

哦,我注意到我读了一本过时的velocity用户指南,但没有详细说明转义可能会失败。他们建议使用
#set($D='$)
,请参阅。在本文档中,``已作为有效的excape字符提到。未分析的内容提示可能在其他情况下有用,例如。