Need help writing Java template

Need help writing Java template

by Mark Stern -
Number of replies: 3

I am trying to write a Java question, which involves some static analysis on the student answer before running the code.

In order to do that, I need to store the student answer in a variable.

I tried:

String studentAnswer = {{ STUDENT_ANSWER | e'java' }};

and

String studentAnswer = "{{ STUDENT_ANSWER | e'java' }}";

but neither of those worked. What is the correct incantation?

Thanks,

Mark Stern




In reply to Mark Stern

Re: Need help writing Java template

by Richard Lobb -

You have a syntax error in calling the escape filter (e). The following should work:

String answer = "{{ STUDENT_ANSWER | e('java')}}";

If that doesn't work, and if you can't figure it out by turning on template debugging, export your question as an xml and attach it to a posting explaining exactly what error you're getting.

In reply to Mark Stern

Re: Need help writing Java template

by Mark Stern -
I have made some progress. I had put the above code inside global extra, but it seems that {{ STUDENT_ANSWER }} does not work there - so I moved the code to the main part of the template. However, it still does not like | e'java'. I get an error message:

TEMPLATE ERROR: Unexpected token "string" of value "java" ("end of print statement" expected)

I also tried other escapes, including e'js' (which is a TWIG built-in, according to the documentation), with similar errors.
In reply to Mark Stern

Re: Need help writing Java template

by Mark Stern -
Thanks, those 2 things together fixed the problem.