4

I am trying to pass an arraylist to a scala template from a play controller.

In my controller

List<Profile> profiles = Profile.findAll();

return ok(contacts.render(profiles));

In the template contacts.scala.html

@import models.com.contactmanager.Profile
@(profiles: List[Profile])

I am getting the error:

not found: value profiles [error] 

for line

@(profiles: List[Profile])

2 Answers 2

7

In Parameter lists of Scala templates you have to use the (a) fully qualified class name or (b) you import them in your Build.scala.

(a)

@(profiles: List[models.com.contactmanager.Profile])

(b)

//Play 2.2
val main = PlayProject(…).settings(
  templatesImport += "models.com.contactmanager.Profile"
)

For Play 2.3 the API changed: https://www.playframework.com/documentation/2.3.x/ScalaTemplates#Import-statements

TwirlKeys.templateImports += "models.com.contactmanager.Profile"
Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer. What is strange is that if the class is right inside "models", you do not have to specify the fully qualified name. If I create a "services" package, the compiler won't see classes inside that.
This is right answer, but i want mention that for play 2.3 behaviour is changed, and templatesImport += should be placed in build.sbt file as TwirlKeys.templateImports +=
1

My guess is that the import statement has to be below the parameter statement.

Try switching the order of

@import models.com.contactmanager.Profile
@(profiles: List[Profile])

to

@(profiles: List[Profile])
@import models.com.contactmanager.Profile

2 Comments

I switched the order of statements as you had suggested, but I am getting the same compile error.
I also have a Play-Project with a Scala-Template and at least i get "not found: value ???" when i switch them like you had them. I'm afraid I don't know then. Can you maybe post the full template?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.