Java Play Framework creating logged in navigation bar - inserting dynamic html in scala template

I am working with Play Framework in Java, creating simple website with OAuth and Bootstrap. My goal is to have navigation bar showing logged in user after authentication - So I need dynamic creation of html code for navigation bar. Many people would instantly suggest using scala template parameters to create that dynamic content:

navigation.scala.html

@(loggedInUser: String)
<div class="navbar">
    <div class="containter">
            <ul class="nav navbar-nav navbar-left">
            <li>Logged in as: @loggedInUser</li>
            </ul>
    </div>
</div>

main.scala.html

@(loggedInUser: String)(content: Html)
<!DOCTYPE html>
<html lang="en">
    <body>
        @navigation(loggedInUser)
        @content    
    </body>
</html>

This seems fine but as application grows there is going to be much more templates which have to call main template, and in every single one I will be passing parameter loggedInUser just so it would reach navigation. It's just becoming big stack of parameters on each template. So is there any way to get this loggedInUser string directly, not by passing it through called templates? I tried to solve it with having static Controller as Utility which would fetch me username which I would do by putting username in session attribute in moment of authorization and then pulling it with "Utility Controller":

public class UsernameController extends Controller {
    public static String getLoggedInUsername() {
        return ctx().session().get("username");
    }
}

and then changing navigation.scala.html to:

@()
<div class="navbar">
    <div class="containter">
            <ul class="nav navbar-nav navbar-left">
            <li>Logged in as: "@routes.UsernameController.getLoggedInUsername()"</li>
            </ul>
    </div>
</div>

But this also doesn't work good because Controller is static, and for e.g. when it comes to Dependency Injection (in my case using pac4j for OAuth) of factory classes, you cannot use static methods (I know there is static injection solutions but it is not recommended for use). I hope there is some way of using methods in scala templates which generate html so I can use it to inject it into navigation.scala.html (method would have access to Session and all attributes so I could get info about the current user)


ANSWERS:


You can use Deadbolt to take care of this, using the @subjectPresent and/or @subjectPresentOr template constraints. The former only allows rendering to occur if a user is logged in, and the latter renders alternative content if no user is logged in, e.g.

@import be.objectify.deadbolt.java.views.html.{subjectPresent, subjectPresentOr}

@subjectPresent() {
    This content will be present if handler#getSubject results in a Some 
}

@subjectPresentOr() {
    This content will be present if handler#getSubject results in a Some 
} {
  fallback content
}

Take a look at the Deadbolt Java documentation for more details.

However, if you need user details to be displayed in the template then you really should be passing that information into the template.

Using <li>Logged in as: "@routes.UsernameController.getLoggedInUsername()"</li> is an approach that is not clean and very possibly not thread-safe. Twirl templates are type-safe and parameterized for a reason!



 MORE:


 ? Dynamic search and filtering in Excel
 ? Dynamic TableView Swift 3 With Beginning Graph
 ? Switch case is not working in Reactjs
 ? Dynamic Polymorphism's weird output
 ? which technologies to use for forms created dynamically?
 ? Winforms - Dynamic Load / Save Settings
 ? Winforms - Dynamic Load / Save Settings
 ? Winforms - Dynamic Load / Save Settings
 ? Save Settings in a .NET Winforms Application
 ? C# Setting load and save