Using curly braces in JavaScript in a Play Framework template

I passed a list of names that I went through from my controller:

@titles:List[String]

I want to cyclically generate some html headers with {mmm} appearing after:

@titles.map { title =>
  <h1>{title} {mmm}</h1>
}

Clearly, there is a problem because it will try to find a variable called mmm. How to avoid parentheses?

Or is there a more idiomatic way to create HTML that is not related to list display? Very new to this!

+5
source share
4 answers

You do not need to avoid curly braces in Play-unlike in Scala XML literals, they have no special meaning in playback patterns except after @.

You should be able to write the following:

@titles.map { title =>
  <h1>@{title} {mmm}</h1>
}

<h1>Whatever Title {mmm}</hq> ..

+4

( ), . , , :

, :

@for(title <- titles){
    Title: @title {mmm} <br/>
}

( Travis)

@for(title <- titles) { 
    Title: @{title} {mmm} <br/> 
}

,

@for(title <- titles){
    Title: @title - <b>something else without bracets</b> <br/>
}

Alternative

toBrackets() ( Java)

public String toBrackets(String string) {
    return "{" + string + "}";
}

:

@for(title <- titles){
    Title: @title @title.toBrackets("mmm") <br/>
}
+3

JS , HTML

&#123; for left curly brace
&#125; for right curly brace

:

{ for left curly brace
} for right curly brace
+3

@LeftBracket= { @RightBracket= }, :

@titles.map { title =>
  <h1>{title} @{LeftBracket}mmm@RightBracket</h1>
}

Side note somewhat related to your question:

My impression is that if the brackets do not match (that is, }for each {),
the template compilation failed, so maybe the @RightBracketonly way to insert one }? This would be useful if you needed to write:

@titles.map { title =>
   hello :-}
}

: -}

+1
source

All Articles