Question Authors' Forum

Running C# in CodeRunner

Picture of Richard Lobb
Re: Running C# in CodeRunner
by Richard Lobb - Sunday, 21 January 2018, 9:11 PM

Hi Martin

Thanks for posting. Just to answer your question about why the | e('py') is necessary....

When wrapping a raw string from the author form or student answer in quotes to make a literal string within the template code, you generally need to escape various special characters that might result in invalid string literals. As a trivial example, if a student's answer was "Hi" (including the double quotes) and you wrote, in JavaScript.


you'd get

s = ""Hi"";

which is broken. Hence Twig provides a range of escapers, so that for a JavaScript template you can write

s = "{{STUDENT_ANSWER | e('js')}}"

which would give you the valid

s = "\"Hi\"";

I've added to the Twig base set of escapers, e('py'), e('java') and e('matlab'). The Java escaper can be used for C and C++ too. 

The Python escaper is intended for use only within triple-quoted string literals. It escapes only double quotes (in case the raw string itself has triple quotes in it) and backslashes. Backslashes need to be escaped so that, for example, a 2-character \n in the student's code doesn't get converted to a single newline character in the triple-quoted literal.