在 PhpStorm 的实时模板中使用数学

Use math in PhpStorm's Live Template

如何在 PhpStorm 的实时模板中使用数学?

例如,我的模板中有 $Image_width$,我想像这样设置高度图像:

<img width="$Image_width$" height="($Image_width$ / 2)">

您可以使用 groovyScript() 函数来定义自定义表达式 - 请参阅 https://www.jetbrains.com/help/phpstorm/template-variables.html#predefined_functions

例如,对于像这样的模板:

<img width="$Image_width$" height="$Image_width_half$">

您可以将 groovyScript("def res=_1 as Integer; return res /2 ", Image_width) 指定为 $Image_width_half$ 变量的表达式:

_1 这里是作为参数传递给 groovyScript() 函数的第一个变量的引用