I was having an issue iterating over a parameter set. I wanted to do this to make multi-line parameters more straightforward to input (rather than using \n). After some head-scratching, I found using the dump() method that the parameter was being passed as a StdObj rather than as an array.
I put a hack into the render function in php.twig like so:
public static function render($s, $student, $parameters=array(), $isstrict=false) {
$twig = qtype_coderunner_twig::get_twig_environment($isstrict);
$parameters['STUDENT'] = new qtype_coderunner_student($student);
if (array_key_exists('__twigprefix__', $parameters)) {
$prefix = $parameters['__twigprefix__'];
$s = $prefix . $s;
}
$template = $twig->createTemplate($s);
$parameters=json_encode($parameters);
$parameters=json_decode($parameters, true);
$renderedstring = $template->render($parameters);
return $renderedstring;
}
The hack encodes to json and decodes back (original idea from here) which then means everything in the parameter string is converted to an array. This means that it is Traversable in Twig and thus can be iterated over. If I add the array extension to Twig, then I should also be able to shuffle the array. Presumably there is something somewhere not returning an array, but I am not that into the code base at the moment.
This then gets me server-side randomising of program lines for my Parson's Problem project.
Is this worth looking at in the main code?
Thanks
Paul