呈现Play!Framework2 JavaScript作为模板? [英] Render a Play!Framework2 javascript as template?

查看:68
本文介绍了呈现Play!Framework2 JavaScript作为模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Play!Framework 2中的模板返回一个.js文件.

I'd like to return a .js file from a template in Play!Framework 2.

这就是我的做法:

在我的views文件夹中,创建了一个模板文件:

In my views folder, I created a template file :

initials.scala.js

包含类似以下内容的

Containinig something like :

{
    'data': @Model.find.findList()
}

然后,在我的控制器中:

And then, in my controller :

public static Result initials() {
    return ok(views.js.initials.render());
}

这对我来说很简单,但是我得到了这个错误:

This is pretty straighforward for me, but I get this error :

错误:软件包views.js不存在

error: package views.js does not exist

我做错了什么?

如果我将js重命名为txt(在文件名和控制器中的包调用中),则一切正常.但是,如果我将其更改为js,则会收到错误消息.

If I rename js to txt (in the filename and the package call in the controller), everything works fine. But if I change it to js, I get an error.

推荐答案

您没做错什么,Play允许您仅将.html.xml.txt扩展名用于视图,因此您需要使用以下一种他们.

You did nothing wrong, Play allows you tu use only .html, .xml or .txt extensions for views, so you need to use one of them.

从技术角度来看,使用哪一个都没有关系,无论如何,您都需要将其作为JavaScript从控制器中返回,即:

From technical point of view it doesn't matter which one you'll use, anyway you need to return it from controller as JavaScript, ie with:

public static Result initials() {
    return ok(views.html.initials.render()).as("application/javascript");
}

这篇关于呈现Play!Framework2 JavaScript作为模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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