I would suggest doing absolutely everything possible under the sun to avoid it. It is deliberately made difficult for security reasons, and it also makes the framework’s life a lot harder. Life goes so much more smoothly if HTML stays in templates.
If you can back up and describe your requirements in a non-technical way, perhaps people can suggest alternative implementation strategies that avoid all the drawbacks of manual sanitization.