Tags
While updating my site to use Markata's new configurable head I ran into some escaping issues. Things like single quotes would cause jinja to fail as it was closing quotes that it shouldnt have.
Jinja Escaping Strings
Jinja comes with a handy utility for escaping strings. I definitly tried to
over-complicate this before realizing. You can just pipe your variables into
e
to escape them. This has worked pretty flawless at solving some jinja
issues for me.
<p> {{ title|e }} </p>
Creating meta tags in Markata
The issue I ran into was when trying to setup meta tags with the new
configurable head, some of my titles have single quotes in them. This is what
I put in my markata.toml
to create some meta tags.
[[markata.head.meta]] name = "og:title" content = "{{ title }}"
Using my article titles like this ended up causing this syntax error when not escaped.
SyntaxError: invalid syntax. Perhaps you forgot a comma? Exception ignored in: <function Forward.__del__ at 0x7fa9807192d0> Traceback (most recent call last): ... TypeError: 'NoneType' object is not callable
jinja2 escape
After making a complicated system of using html.escape
I realized that jinja
included escaping out of the box so I updated my markata.toml
to include the
escaping, and it all just worked!.
[[markata.head.meta]] name = "og:title" content = "{{ title|e }}"