我想使用Play Framework在Scala Views中创建基于子文件夹的菜单。
如何为scala模板提供变量以供用户使用,例如用户,菜单和其他,并使其工作?目前的设置不起作用。如何生成一个菜单,根据目录结构动态抓取当前视图的子节点?
# Test
GET /admin/:page controllers.test.TestController.index(page)
GET /components/:page controllers.test.TestController.index(page)
我认为它最终会如何运作的例子:
public class TestController extends Controller {
public Result index(String page) {
switch (page) {
case "test1" :
List<Role> allRoles = Role.find.all();
User user = getCurrentUser();
Menu menu = Menu.test();
return ok(test1.render(allRoles, user, menu));
case "test2" :
return ok(test2.render());
default :
return notFound();
}
}
}
Views (Folder)
|
+-- Tools (Folder)
| |
| +-- Admin (Folder)
| | |
| | +-- roles.scala.html
| | +-- users.scala.html
| | +-- addUsers.scala.html
| |
| +-- Components (Folder)
| | |
| | +-- roles.scala.html
| | +-- users.scala.html
| | +-- addUsers.scala.html
| |
| +-- someFile.scala.html
| +-- someFile2.scala.html
|
+-- main.scala.html
@(title: String, scripts: Html = Html(""))(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>SP81D</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Load site-specific customizations after bootstrap. -->
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="@routes.Assets.at("javascripts/production/ie.min.js")"></script>
<![endif]-->
</head>
<body>
<header class="wrapper fixed-header" role="banner">
<div class="container-fluid">
<!-- Site Title -->
<div class="site-title col-xs-3">
LOGO HERE
</div>
<!-- Site Navigation -->
<div class="top-components">
SEARCH
</div>
</div>
</header>
@content
<footer class="footer">
<div class="container">
<p class="text-muted">Place sticky footer content here.</p>
</div>
</footer>
<script src="@routes.Assets.at("javascripts/production/production.js")"></script>
@scripts
</body>
</html>
@(roles: List[Role])
@(user: User, menu: Menu)
@import helper._
@Main("identification", scripts) {
<div class="table-responsive">
<table id="roles_table" class="table table-striped">
<thead>
<tr>
<th>Role</th>
</tr>
</thead>
<tbody>
@for(index <- 0 until roles.size) {
<tr>
<td>@roles(index).getName()</td>
</tr>
}
</tbody>
</table>
</div>
}
@scripts = {
<script>
$(document).ready(function ($) {
$('#roles_table').dynatable();
});
</script>
}
此示例文件当前不起作用,因为我需要一种方法将一些隐式数据输入到所有模板中,并且它不能正常工作。
cannot find symbol
symbol: method getCurrentUser()
location: class controllers.test.TestController
not found: value scripts
@Main("identification", scripts) {
<div class="table-responsive">
<table id="roles_table" class="table table-striped">
<thead>
<tr>
现在修复了所有错误,但我无法想到如何根据视图目录中的文件集以及可能通过权限动态生成菜单。
唯一想到的是生成一个包含URL,链接名称,权限设置和父链接的表,并从该表生成菜单。