在freemarker中是否可以在包含文件之前检查文件是否存在? [英] In freemarker is it possible to check to see if a file exists before including it?

查看:81
本文介绍了在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:

  1. 如果文件不存在,请不要调用包含文件-因此需要检查文件是否存在.

-或-

  1. 一种在不更改日志记录的情况下防止在recovery块中记录错误的方法,以防止显示所有freemarker错误.

推荐答案

更简单的解决方案是:

<#attempt>
    <#import xyz.ftl>
    your_code_here
<#recover>
</#attempt>

这篇关于在freemarker中是否可以在包含文件之前检查文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆