在freemarker中是否可以在包含文件之前检查文件是否存在?
问题描述:
我们正在尝试在freemarker中构建一个系统,在该系统中可以选择添加扩展文件来替换标准模板的块.
We are trying to build a system in freemarker where extension files can be optionally added to replace blocks of the standard template.
我们到此为止
<#attempt>
<#include "extension.ftl">
<#recover>
Standard output
</#attempt>
因此-如果存在extension.ftl文件,则将使用该文件,否则将输出restore块内的部分.
So - if the extension.ftl file exists it will be used otherwise the part inside of the recover block is output.
此问题是,freemarker总是记录导致恢复块触发的错误.
The problem with this is that freemarker always logs the error that caused the recover block to trigger.
所以我们需要以下两点之一:
So we need one of two things:
- 如果文件不存在,请不要调用包含文件-因此需要检查文件是否存在.
-或-
- 一种在不更改日志记录的情况下防止在recovery块中记录错误的方法,以防止显示所有freemarker错误.
答
更简单的解决方案是:
<#attempt>
<#import xyz.ftl>
your_code_here
<#recover>
</#attempt>