vendredi 17 juin 2016

Menu Generation and Dynamic Routes Java/Scala Play Framework

I have tried searching for about an hour and tried a few things that didn't quite work. I would also preface that this is my first experience with Play and finding the documentation a bit rough, and I did sudo code some of this because I'm not sure what I'm allowed to post and what I'm not.

Also this post seems very complex so if something doesn't make sense please ask before throwing the down vote and I'll amend the post asap.

I want to create menus based on sub-folders in Scala Views in the Play Framework.

Question :

How can I feed in variables for the scala templates to use such as User, Menu, and Other and make it work. The current setup isn't working. And how do I generate a menu that dynamically grabs the child of the current view based on the directory structure.

Routes Specific File

 # Test
 GET     /admin/:page                 controllers.test.TestController.index(page)

 GET     /components/:page                 controllers.test.TestController.index(page)

Controller Example of how I'm thinking it will work in the end.

public class TestController extends Controller {
    public Result index(String page) {
        switch (page) {
            case "test1" :
                return ok(test1.render());
            case "test2" :
                return ok(test2.render());
            default :
                return notFound();
        }
    }
}

Views Directory

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

Main Scala File

@(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>

Sample Admin File

@(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>
}

This sample file doesn't currently work because I need a way to enter some implicit data into all templates and it isn't working well.

Aucun commentaire:

Enregistrer un commentaire