简体   繁体   中英

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:

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%> ? Thanks in advance!

FreeMarker templates can only display what you give to them through the data-model (aka. the template context). After all, a template is an MVC View, not a general purpose program. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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