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 8919353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:59:59+00:00 2026-06-15T05:59:59+00:00

I just started messing around with Play 2 . I recently learned how to

  • 0

I just started messing around with Play 2. I recently learned how to create a model (User) and how to show it in the view (please let me know if there’s any bad practice):

models/User.scala:

package models

case class User(id: Long, name: String)

object User {
  var user = User(
    id = 1L,
    name = "Mark"
  )

  def greeting = TODO

}

controllers/Application.scala:

package controllers

import play.api._
import play.api.mvc._
import models.User

object Application extends Controller {

  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

  def hello =  Action {
    Ok(views.html.hello(User.user))
  }

}

hello.scala.html:

@(user: User)

@main("Welcome to Play 2.0") {

  <h2>@user.id</h2>
  <h3>@user.name</h3>

}

Now, I want to display the output of a function in the view.

How to accomplish that?

  • 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-15T06:00:00+00:00Added an answer on June 15, 2026 at 6:00 am

    Let’s use another view (tag) which as you probably know is also just a Scala function:

    /app/views/tags/userName.scala.html

    @(user: User)
    My Name is @user.name
    

    So you can use the userName function in your index view

    @(user: User)
    
    @main("Welcome to Play 2.0") {
    
        <h2>@user.id</h2>
        <h3>User @user.name said: @tags.userName(user)</h3>
    
    }
    

    De facto the same you can also use functions from your models and controllers by just specyfying it as:

    @SomeController.someFunction(user)
    @SomeModel.someFunction(user)
    

    And if your class isn’t placed in controllers or models packages (which are imported into the views by default) you just need to use full qualified path:

    @utils.MyDateFormaters.formatDay(user.birthDay)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started messing around with php recently and I was testing my new
I've recently started messing around with Java sockets and telnet... I want the user
I just started learning about servers, and I am messing around with Google's App
I've been messing around with javascriptmvc. After getting data from the model, the view
I just started messing around with having my application store sessions in the database
I just started messing around with C#/.NET/mono and stuff, and I'm trying to make
So I just started messing around with Apache's rewrite module and would like to
I just started messing around with the lastfm API and I'm not sure why
I just started messing around with AJAX in an attempt to validate a form
I've been messing around with this for some time. I recently started using the

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.