Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exercises

  1. Hello World
  • Create a Twirl template that accepts a string parameter name and displays "Hello, name!" in an HTML paragraph tag.
  1. List Rendering
  • Make a Twirl template that takes a List of strings and renders each string in an unordered list (<ul>). Ensure to handle an empty list gracefully by displaying a friendly message.
  1. Form Submission Display
  • Develop a form in Twirl that posts a user's first and last name to a Play controller. Then, display the submitted names on a new page using another Twirl template.
  1. Conditional Content
  • Design a Twirl template that accepts a Boolean value. Depending on the value, it should display different messages or HTML elements.
  1. Nested Templates
  • Create a main layout template that includes a header, footer, and a placeholder for content. Then, create a child template that extends this layout and populates the content area with specific information.
  1. Loop with Conditional
  • Write a Twirl template that takes a list of integers. Display each number in a list item (<li>). Highlight numbers that are divisible by 3 by making them bold.

Tips for Getting Started

  • To add a Twirl template to your Play project, create a file with the .scala.html extension in the app/views directory.
  • Pass data to your Twirl templates by adding parameters to the template function declaration. For example, to pass a name to your template, start the file with @(name: String).
  • Use Scala control structures directly in your templates for loops and conditionals.