You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:11:54