0
votes

Im fairly new to using Play! and to using scala, I cant figure out how I pass in a custom class type to a view in play! 2.2.x.

What I want to accomplish is:

Have a base class (such as)

public abstract class Category {
    String Name;
    String Info;
    String Link;
}

Pass a number of these to the view:

@(categories: Array[Category])

@main{
{
    //In here, iterate over all the categories and display their information.
}

With use of a controller

public static Result categories() {
    Category[] categoriesArray = new Category[3];
    categoriesArray[0] = new blahCategory(...);        
    categoriesArray[1] = new fooCategory(...); 
    categoriesArray[2] = new someCategory(...); 

    return ok(categories.render( categoriesArray ));
}

I have the abstract class under a folder called "model" and the scala.html files in the "view" folder.

However when going to the url, play! errors on:

not found: type Category
In /home/me/MySite/app/views/categories.scala.html at line 0.

1 @(categories: Array[Category])
2 
3 @main{
4 @heading()
5 {
6 }
2

2 Answers

2
votes

I think you either need to fully qualify the class name

@(categories: Array[model.Category])

Or import the model package into your template

@import model._
@(categories: Array[Category])

You can also define imports common to all your template in project/Build.scala

val main = PlayProject(…).settings(
    templatesImport += "model._"
)

From the documentation

0
votes

Add your class to the templatesImport setting in your build file.

http://www.playframework.com/documentation/2.2.x/ScalaTemplates