Following the suggestions below, I have suceeded in getting both a Rules version and a Twig version to work. I have settled on the Twig version as it seems to run a little faster.
The Twig parser also crashes if I do not use the purge command in the twig_handler routine, so there is an undocumented limit there as well.