简体   繁体   English

FreeMarker可以将目录的内容写入模板吗?

[英]Can FreeMarker write contents of directory to a template?

I'm trying to figure out how to use FreeMarker to generate a file like so: 我试图弄清楚如何使用FreeMarker生成文件,如下所示:

The contents of /home/myuser/somedir are:
    blah.txt
    fizz.gif
    buzz.jpg
    widget.log

...from a template like so: ...从这样的模板中:

The contents of <%dir%> are:
    <%contents%>

I read the excellent Vogella tutorial on FreeMarker, but not really sure how to put this all together: once I query the directory for its contents, how do I add each item to this list of <%contents%> ? 我在FreeMarker上阅读了出色的Vogella教程,但是并不确定如何将所有内容放在一起:查询目录的内容后,如何将每个项目添加到此<%contents%>列表中? Thanks in advance! 提前致谢!

FreeMarker templates can only display what you give to them through the data-model (aka. the template context). FreeMarker模板只能显示您通过数据模型(即模板上下文)提供给他们的内容。 After all, a template is an MVC View, not a general purpose program. 毕竟,模板是MVC视图,而不是通用程序。 So, create a List<String> before you call FreeMarker, call it dirContents or something, put that into the data-model, also the directory name, and call it dirName , and then do something like: 因此,在调用FreeMarker之前,先创建一个List<String> ,将其dirContents或类似的东西,然后将其放入数据模型中,也将其放入目录名,然后将其dirName ,然后执行类似的操作:

<p>${dirName}:</p>
<ul>
  <#list dirContents as entry>
    <li>${entry}</li>
  </#list>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM