-2

In a twirl template I'm trying to render a loop of strings and the current code is giving me code errors. I'm sure it's a small detail that I'm missing...

This is my template...

@(title: String)(content: Html)
<!DOCTYPE html>
<html lang="en">
    <head>
        @* Here's where we render the page title `String`. *@
        <title>@title</title>

    </head>
    <body>
        @content
</script>
    </body>
</html>

And this is where I use this template and try to do a for loop...

@(names: ("George", "John", "Ally"))

@list("List of names")  {
  <h1>Test</h1>
  <ul>
    @for(name <- names) {
      <li>@name</li>
    }
  </ul>
}

And my controller...

package controllers;

import play.mvc.*;

public class NewController extends Controller {
    public Result newPage() {
        return ok(views.html.newPage.render());
    }

}

Here is the error....

Compilation error
':' expected but identifier found.

In G:\Github\playExample - Copy\example\app\views\newPage.scala.html:10
6    @for(name <- names) {
7      <li>@name</li>
8    }
9  </ul>
10} 

What am I missing?

Emmanuel Henri
  • 153
  • 3
  • 27
  • 2
    You are missing the error message you are getting... – cbley Apr 09 '20 at 20:05
  • Seconded — do you get a compile-time error message, a runtime error, other unexpected runtime behavior, or what? – Seth Tisue Apr 09 '20 at 22:30
  • without the error is not easy to understand what's your problem... I'll leave this working example of a loop in twirl, hopefuly it can help you in the meanwhile: https://github.com/pedrorijo91/play-slick3-steps/blob/master/app/views/index.scala.html#L121-L129 – pedrorijo91 Apr 10 '20 at 11:28
  • @cbley I just added the error.... – Emmanuel Henri Apr 10 '20 at 13:40
  • @SethTisue see above – Emmanuel Henri Apr 10 '20 at 13:40
  • I also tried the following `@(names: Array[String] = Array("George", "John", "Ally"))` in the template and it gives me ```method render in class views.html.newPage cannot be applied to given types; required: java.lang.String[] found: no arguments``` – Emmanuel Henri Apr 10 '20 at 14:52

1 Answers1

1

Your question is still very confusing.

I'm going to recommend you to see this project again https://github.com/pedrorijo91/play-slick3-steps (or any simple play project).

You tag this as Scala, but your controller is clearly a java controller.

In the controller your should have something like:

class Controller @Inject()(val controllerComponents: ControllerComponents) extends BaseController {

  def newPage() = Action { implicit request: Request[AnyContent] =>
    Ok(views.html.newPage())
  }

Then in the view, you seem to have the first template ok, but I don't understand the second part you added. Is it a partial? what's the file name? where/how is it being called?

I would expect something like this:

@(names: List[String])

<h1>Test</h1>
<ul>
    @for(name <- names) {
        <li>@name</li>
    }
</ul>

I don't understand the syntax you are using

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82