【发布时间】:2013-03-17 12:50:55
【问题描述】:
我正在使用 FreeMarker 模板引擎从 Web 服务的抽象描述中生成一些 php 类。我的问题是,当我在 FreeMarker 模板中调用宏时,宏会在宏调用之前插入没有左侧空格的文本。
exampleTemplate.ftl:
<?php
class ${class.name} {
<@docAsComment class.doc/>
<#list class.fields as field>
$${field.name};
</#list>
<#-- ... -->
}
?>
<#macro docAsComment doc>
/*
<#if doc.title != "">
* ${doc.title}
</#if>
<#list doc.content as content>
<#if content != ""> * ${content}</#if>
</#list>
*/
</#macro>
这将生成如下内容:
<?php
class foo {
/*
* foo
* bar foo, bla
*/
$a;
$b;
}
?>
一种解决方案是将前导空格作为参数提交给宏,但这只会使模板更加不可读。有没有更好的解决方案?
【问题讨论】:
标签: java code-generation freemarker