是否可以将VirtualFile用作HTML模板提供程序?如何?

时间:2012-08-14 00:32:18

标签: java playframework playframework-1.x

需要你的帮助!

我希望标题是描述性的,但这里是详细信息

假设我们有5个Web应用程序,并且可以说它们已经完成了5个不同的框架。其中一个是使用playframework。现在,由于重塑所有五个网站将花费大量时间,我们开始想要将一个布局模板作为HTML页面托管,其中包含<%doLayout%>在其中播放标签。

我们需要使用此布局,因为它是播放应用程序源代码的一部分。现在我阅读API文档并找到VirtualFile类(VirtualFile API Docs)。由于文档非常简短,我不能从文档中说出这个类的目的是什么,但我发现它包含的功能对我的案例很有用......

下面是相同的类,但在.NET中。我之前做过这样的集成,但现在我也需要在playframework中使用它。另请注意,下面的链接示例来自zip存档,但使用VirtualPathProviders,您可以从任何地方提供模板 Virtualizing Access to Content

任何帮助都表示赞赏,因此,解决方案不需要包含VirtualFile,但它必须是即插即用。

注意:我们正在使用playframework 1.2.1

由于

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法,但没有找到虚拟文件类。如果我在游戏中创建自定义标记,它将取代我的视图中的extend标记。

如果您查看播放源代码,您可以找到现有的工作原理 FastTags class, line 319

public static void _extends(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
    try {
        if (!args.containsKey("arg") || args.get("arg") == null) {
            throw new TemplateExecutionException(template.template, fromLine, "Specify a template name", new TagInternalException("Specify a template name"));
        }
        String name = args.get("arg").toString();
        if (name.startsWith("./")) {
            String ct = BaseTemplate.currentTemplate.get().name;
            if (ct.matches("^/lib/[^/]+/app/views/.*")) {
                ct = ct.substring(ct.indexOf("/", 5));
            }
            ct = ct.substring(0, ct.lastIndexOf("/"));
            name = ct + name.substring(1);
        }
        BaseTemplate.layout.set((BaseTemplate) TemplateLoader.load(name));
    } catch (TemplateNotFoundException e) {
        throw new TemplateNotFoundException(e.getPath(), template.template, fromLine);
    }
}

我只需要使用重载方法TemplateLoader.load(String, String)接受模板作为字符串(与原始实现中的文件名比较),然后将其设置为 BaseTemplate.layout.set(...)方法。当然,首先我需要通过HTTP请求从远程服务器获取模板。

相关问题