In the previous section, I returned the string to the customer via OK (). I can put a complete HTML page into the string to return. However, modern frameworks provide a better way--templates. The template separates the view from the data. The server can pass different data to the same template, resulting in different pages. Play also has a template system. Most of the template's content can be written in HTML, as a view, and in some special place, the data parameters are reserved. In the template, use the Scala language to invoke the arguments.
Working with templates
I first create a template that is purely view-capable. In the App/views folder, create a new file index.scala.html (delete and recreate it if you have one).
<! DOCTYPE html>
This template is a pure HTML file and is the simplest form of template.
Modify App/controllers/application.java:
package controllers;
Import play.*;
Import play.mvc.*;
public class Application extends Controller {public
static result index () {return
OK (views.html.index.render ());
}
}
Ok () is receiving views.html.index.render (), which is actually the render () method of the app/views/index.scala.html template. Play will automatically generate the corresponding class according to the template.
You can also use import to introduce Views.html.index, rather than using the full classpath.
Access page: