Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7734965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:22:58+00:00 2026-06-01T07:22:58+00:00

My code in scala template: @session.get(user.id) match { case Some(_) => xx case _

  • 0

My code in scala template:

@session.get("user.id") match {
    case Some(_) => "xx"
    case _ => "yy"
}
<a href="">Logout</a>

But the case ... be displayed directly to the generated html page:

match { case Some(_) => "xx" case _ => "yy" }  Logout

And In the generated .template.scala, it’s:

"""
<body>
"""),_display_(Seq(/*11.4*/session/*11.11*/.get("user.id"))),format.raw/*11.26*/(""" match """),format.raw("""{"""),format.raw/*11.34*/("""
    case Some(_) => "xx"
    case _ => "yy"
"""),format.raw("""}"""),format.raw/*14.4*/("""
<a href="">Logout</a>
"""

But I see in the doc, it should support the match case: https://github.com/playframework/Play20/wiki/ScalaTemplates

@connected match {

  case models.Admin(name) => {
    <span class="admin">Connected as admin (@name)</span>
  }

  case models.User(name) => {
    <span>Connected as @name</span>
  }

}

UPDATE1

Finally, I found a way to work:

@defining(session.get("user.id")) { x =>
    @x match {
        case Some(_) => { "xx" }
        case None => {"yy"}
    }
}

But it looks so complicated.

UPDATE2

Find another simple solution:

@{session.get("user.id") match {
    case Some(_) => "xx"
    case _ => "yy"
}}

But it doesn’t work well in complex case:

@{session.get("user.id") match {
    case Some(_) => {<a href="@routes.Users.logout">Logout</a>}
    case _ => "yy"
}}

The @routes.Users.logout won’t be converted.

UPDATE3

This is a getOrElse solution:

@session.get("user.id").map { _ =>
    <a href="@routes.Users.logout">Logout</a>
}.getOrElse {
    Not logged
}

It works but it doesn’t use match case

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:23:00+00:00Added an answer on June 1, 2026 at 7:23 am

    I was hitting the same problem. Enclosing the right part of the case in curly braces fixed the issue for me.

    This works for me:

    @user match {
        case Some(user) => { Welcome, @user.username! }
        case None => { <a href="@routes.Application.login">Login</a> }
    }
    

    Without the braces, it gave an error with the space after the { on the match line highlighted. “‘case’ expected but identifier found.”

    I also gives me that error if I try to put an @ before the opening curly brace like this:

    //This gives me the same error
    @user match {
        case Some(user) => @{ "Welcome, " + user.username + "!" }
        case None => { <a href="@routes.Application.login">Login</a> }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I meet some scala code with ? but do not know what it mean
I saw some scala code that assign _ to a field of class, what
Just going through the sample Scala code on Scala website, but encountered an annoying
I am trying to run some Scala code on my OSX machine and keep
My company is beginning to write some code using Scala. I've been moved onto
I have code in scala template like: @for(col <- List.range(0,12)) { <td> @if(col %
I am trying to write some Scala code that needs to do something like:
Consider the following two fragments of code: scala> def f1(x:Any) = x match {
I'm just beginning in scala and I'm converting some java code into scala and
I have a template class that's a simple vector, but this piece of code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.