1

In the controller we are creating a string which shall be interpreted as Html when the page is being rendered. Whenever the string contains scala/twirl code starting with "@" it leads to the page not rendering it correctly/at all.

Controller:

// render method
return ok(Html.apply(testButton()), testForm);

public String testButton() throws SQLException {
    result = "<input type='radio' id='@TestForm(\"TestID\").id'   
    name='@TestForm(\"TestID\").name'  value='5'  >" + "Test"; 
    return result;
}

Scala.html:

@(buttons: Html)(TestForm: Form[TestForm])

@buttons

How it should look:

<input type='radio' id='TestID' name='TestID'  value=5  >test

How it looks:

<input type='radio' id='@TestForm("TestID").id' name='@TestForm("TestID").name'  value='5'  >test

We also tested this with other examples. The problem really seems to be the @. Maybe the parser parses the site once, replaces the @button with our code, but doesn't parse that after it. We also tried escaping the @ with different methods(@@, \@, no @), but it always ended up in plain text afterwards.

What is the easiest method to get an @ inside another @ to render?

1
  • 2
    Why don't you want to create reusable view for button instead of creating (I would say clumsy) string? Commented Mar 6, 2017 at 13:06

1 Answer 1

3

You can't do this from within a controller. @ is parsed by the Twirl compiler at compile-time, but you're trying to introduce it at run-time. It will never work. Even if you could get it to work, it wouldn't be a good idea. It breaks the MVC paradigm by muddling the controller code with presentation code.

This should be another Twirl view that looks something like:

// I don't know what TestForm is, so this is a guess
// that it exists in the controller and needs to be passed in
@(TestForm: Form) 

<input
  type='radio'
  id='@TestForm("TestID").id'   
  name='@TestForm("TestID").name'  value='5'  
>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the insight. but how are we supposed to call this view a flexible amount of times with flexible values ?
What does that mean? A Form is a very general object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.