Play Framework
sbt.bat
conf
public
target
Routing settings in a file called routes
(Link the combination of the type of request sent from the client to the server and the URL to the method of the program)
When you access the root (localhost: 9000 by default) with the GET method,
The ʻindex method of the
HomeController class in the ʻapp / controllers
folder is called
conf/routes
GET / controllers.HomeController.index
↓
scala:app/views/index.scala.html
@()
@main("Welcome to Play") {
<h1>Welcome to Play!</h1>
}
Call template index by index method Execute the main function. (As an argument
scala:app/views/main.scala.html
*@
@(title: String)(content: Html)
<!DOCTYPE html>
<html lang="en">
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png ")">
</head>
<body>
@content
<script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
</body>
</html>
Further call main by template index
To embed the passed variable in Html, use @variable name
.
Use the information described in routes
routes
GET / controllers.HomeController.index
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
GET /viewtest controllers.ViewTestController.index #I want to put a link here
In html
<a href="@routes.ViewTestController.index">view test</a>
To do.
" "
, it works properly as a Twirl template.routes
, it iscontrollers. ~~
, But when you paste the link, change it to routes. ~~Suppose you have a template like this.
HTML:viewtest_index.scala.html
@(title: String="File created by myself")
@viewtest_main("File created by myself"){
<h1>@(title+"is")</h1>
}
I am trying to receive a variable called title, but since the initial value is set, the controller
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
@Singleton //Designation of "singleton". Instruction that "only one object can be created from this class"
class ViewTestController @Inject()(cc: ControllerComponents)
extends AbstractController(cc) { // @Inject Instruction to the inside of the framework that "cc is made by the framework"
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.viewtest_index())
}
}
This description is OK.
However
@(title: String="File created by myself")(rnum: Int = 1)
@viewtest_main("File created by myself"){
<h1>@(title+"is")</h1>
<p>The end is@Great chance for rnum! !!</p>
In this way, if two variables are defined in the template and they have initial values, in the controller, ** You have to tell us that you don't pass a value to either of the two variables and leave the default value **. In that case,
Ok(views.html.viewtest_index())
Not (an error occurs)
#### **`Ok(views.html.viewtest_index()())`**
In this way, you have to write two () (for the number of variables) in the method call to convey the intention to the program.
At the top of the template, in @ (title: String =" File you created ") (rnum: Int = 1)
,
The controller's views.html.viewtest_index () ()
supports it, isn't it?
And even if you specify the value of a variable from the controller without using the initial value, arrange two () as many as the number of variables.
Ok(views.html.viewtest_index("Title passed from the controller")(5))
Also, on the template side, how to receive variables
@ (title: String =" File created by myself ", rnum: Int = 1)
If you put them in one () separated by commas like this, the way of passing is also
ʻOk (views.html.viewtest_index ("title passed from controller", 5) `
In this way, it becomes "writing like a normal function".
** Need to be used properly depending on the situation ** How to arrange ()
Because it has the characteristic.
@(title: String= "for loop practice", topics: Array[String])
<ul>
@for(topic <- topics) {
<li>@topic</li>
}
</ul>
The location of @ is a little confusing.
Recommended Posts