Introduction💁
Scala is a computer language for creating robust static systems and functional programs. It is JVM-based and object-oriented. It is capable of interacting with libraries and existing Java programs. The idea of primitive data is absent. Thus it is widely believed to be a static type of language.

Twirl Template Engine 🌀
Now you might be wondering what actually are template engines. So template engines are basically used when there is a need to build a web application having multiple components. They are used to enable fast server-side rendering. For instance, your web application may have various components such as navigation, body, footer, header, etc. So a template engine might help you write codes faster.
Twirl Template Engine is a very powerful template engine that is inspired by the designs of ASP.NET. Some of the advantages of using the twirl template engine are,
-
Twirl Template Engine is a compact, fluid, and expressive template engine.
-
Twirl Template Engine is very easy to learn and quite similar to your traditional coding.
-
Twirl Template Engine is compatible with all the text editors.
- It is not a new language, and Scala developers will find it quite relatable.
Syntax
Before we dive into the syntax of the twirl template engine, let us look at the basic code structure for the twirl template engine.
@(student: Student, subjects: List[Subject])
@* Displaying the name dynamically fetched using Scala *@
<h1>Welcome @student.name!</h1>
@* Displaying all the values present in the Subject list *@
<ul>
@for(subs <- subjects) {
<li>@subs.title</li>
}
</ul>
The above code will be saved with the scala.html extension.
Now, let us discuss some of the commonly used and needed syntax present in the twirl template engine.
The @ Character
The @ character is probably the most important character in Scala Development. It is used to dynamically fetch the values and incorporate them into the HTML page. For instance, in the above code, we used the @ character to fetch the name of the student and the subjects present in the list. Let us look at yet another example for better understanding.
Welcome @(Student.FirstName + Student.LastName)
Parameters
We can also pass some parameters in the HTML code using Scala. For instance, we could declare a variable named “company_name” and assign it the value “CodingNinjas”.
@(title: String = "CodingNinjas")
Iteration In Scala

Similar to other programming languages, we can use the for keyword to iterate over the elements present in the list. For instance,
@* Displaying all the values present in the Subject list *@
<ul>
@for(subs <- subjects) {
<li>@subs.title</li>
}
</ul>
Conditional Statements

Similar to other programming languages, we can use the if-else keyword for conditional statements. For instance,
@if(subjects.isEmpty) {
<h1>Student has not registered for any courses</h1>
} else {
<h1>Student has registered for @items.size courses!</h1>
}
Reusable Blocks
Reusable blocks are used to store content that might be used multiple times in the code later on. We can declare a reusable block in Scala using the following code snippet,
@* A reusable block to capitalise all the starting letters in a word. *@
@capitalEveryWord(text: String) = @{
text.split(' ').map(_.capitalize).mkString(" ")
}
@* Passing the text hello world to capitalise all the first letters. *@
<h1>@title("hello world")</h1>
Comments
Adding comments can be very necessary for your code to make sense. We can add comments in Scala using the following code snippet,
@*
Anything in this section will be treated as a comment.
*@
String Interpolation
String interpolators are basically used when we need to incorporate a variable in a string message. We can use the String interpolators in Scala by importing the following package,
import play.twirl.api.StringInterpolation
An example of String Interpolation would look something like this,
val name = "Your Name"
println(s"Hello, $name") // Hello, Your Name
Scala Types
We can use the concept of wrapping in the twirl template engine. For example,
<ul>
<li>@Option("value inside option")</li>
<li>@List("first", "last")</li>
<li>@User("Foo", "Bar")</li>
<li>@List("hello", User("Foo", "Bar"), Option("value inside option"), List("first", "last"))</li>
</ul>
