1

I need to create an array in one of my index.scala.html. Later I want to use that array to store some values.

I want to achieve following index.scala.html

@import scala._

@myArray = @{ArrayList()};

and I am getting an error saying

not found value @myArray

If above problem is resolved I want to reuser @myArray to add String values. How I can do that ?

thanks

1 Answer 1

4

You can declare and use a new variable like this:

@import java.util._

@defining(new ArrayList[String]()) { myArray =>
    @{
      myArray.add("1")
      myArray.add("2")
      myArray.add("3")
      ""
    }

    @for(s <- myArray) {
        @s
    }
}

But I definitely don't recommend it because it makes your template code a mess. Do it in a controller.

Sign up to request clarification or add additional context in comments.

1 Comment

100% agree about controller, also you can add a method in your model like in computer-database Java sample

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.