Please explain the code below:
@title(text: String) = @{
text.split(' ').map(_.capitalize).mkString(" ")
}
<h1>@title("hello world")</h1>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A breakdown of the reusable code block
@title(text: String)text.split( ' ' )separates the text into a List by splitting the string by ‘ ‘, e.g. “hello world” would become [“hello”, “world”]map(_.capitalize)iterates the List, callscapitalizeon each element, and returns the new List, e.g. [“hello”, “world”] becomes [“Hello”, “World”]. This blog post give a good overview of _.mkString(" ")converts the List back to a String by joining the string with ” “, e.g. [“Hello”, “World”] becomes “Hello World”In summary,
@title(text: String)capitalizes all words in a String.The
<h1>@title("hello world")</h1>is how you could ouput the result in a ScalaTemplate.