Migrating from Flask/Jinja2 to Sanic and Jina2 and url_for in templates

So, looking at porting my app from flask to Sanic. For better or worse, I had written my initial flask app with jinja2 templates that contain url_for which seemed like a good idea at the time…now, not so much. I have url_for working just fine in the python code.

Jinja parses and hits the url_for and asks me what I’m smoking as it doesn’t recognize that. Anyone offer me any tips to mitigate this issue besides a lot of search/replace with “real” html links in my templates?

If I were you, and assuming that the url_for logic doesn’t do the same thing as Sanic’s url_for, I would:

  • Do a search and replace for url_for to my_url_for or another name that makes sense to you
  • Write your custom jinja extension that will process my_url_for() in templates following whichever logic that you need to have
  • Technically speaking, you could modify how jinja handles url_for, which I believe is just mapping to Sanic’s implementation. See source. But you would save yourself a lot of confusion a year down the road or working with others that you have something custom vs overriding the default behavior, imo.

Thanks for your input. I’m going with a lot of search/replace atm. Seems that it should be less upkeep and confusion in the long run.