PlayFramework[Scala]:如何给main.scala.html传入标题内容外的其他参数
Absolutely! You absolutely can pass additional parameters to your main.scala.html template when rendering other pages—this is a core feature of Play Framework's template system (I’m assuming you’re using Play since you’re working with .scala.html files). Here’s a step-by-step breakdown of how to make this work for displaying the user ID in your sidebar:
1. Update main.scala.html to accept the user ID parameter
First, modify your parent template to explicitly accept the user ID as a parameter, along with the content block that gets injected from child pages. Here’s a simple example:
@(userId: String)(content: Html) <!DOCTYPE html> <html> <head> <!-- Your existing head content (styles, scripts, etc.) --> </head> <body> <div class="sidebar"> <!-- Display the user ID prominently in the sidebar --> <div class="user-info"> Current User ID: <strong>@userId</strong> </div> <!-- Your existing sidebar menu items go here --> </div> <div class="main-content"> @content <!-- This is where child page content gets inserted --> </div> </body> </html>
If you need to pass multiple parameters (like username alongside user ID), just expand the parameter list:
@(userId: String, userName: String)(content: Html)
2. Pass the user ID from child templates to main.scala.html
Each child template (e.g., dashboard.scala.html, profile.scala.html) will need to receive the user ID from your controller, then pass it along when invoking the main template.
For example, a dashboard.scala.html child template:
@(userId: String) @main(userId) { <!-- Your dashboard-specific content --> <h1>Welcome to Your Dashboard</h1> <p>Here’s your personalized content for user @userId.</p> <!-- Rest of the page content --> }
If using multiple parameters, you can use named parameters for clarity:
@(userId: String, userName: String) @main(userId = userId, userName = userName) { <h1>Hi, @userName!</h1> <!-- Content --> }
3. Supply the user ID from your controller
In your Play controller, retrieve the user ID (from session, database, authentication context, etc.) and pass it to the child template when rendering:
class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController { def dashboard() = Action { implicit request => // Example: Fetch user ID from session (adjust based on your auth setup) val userId = request.session.get("userId").getOrElse("guest") // Pass the user ID to the child template, which forwards it to main Ok(views.html.dashboard(userId)) } }
Bonus: Use implicit parameters to avoid repetitive passing
If the user ID is needed across nearly all pages, you can use implicit parameters to skip typing it every time.
First, update main.scala.html to accept an implicit user ID:
@(content: Html)(implicit userId: String) <!DOCTYPE html> <html> <!-- ... rest of your template ... --> <div class="sidebar"> <div class="user-info"> Current User ID: <strong>@userId</strong> </div> </div> <!-- ... --> </html>
Then, adjust your child template to expect an implicit user ID:
@()(implicit userId: String) @main { <h1>Welcome to Your Dashboard</h1> <p>User ID: @userId</p> }
Finally, make the user ID implicit in your controller:
def dashboard() = Action { implicit request => implicit val userId = request.session.get("userId").getOrElse("guest") Ok(views.html.dashboard()) // No need to pass userId explicitly now }
内容的提问来源于stack exchange,提问作者user8574635

