Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Jinja2 incompatible with utf-8 with bom #167

kid143 opened this Issue Dec 4, 2012 · 2 comments


None yet
3 participants

kid143 commented Dec 4, 2012

When using utf-8 with bom as template file encoding, jinja seems to treat bom characters like normal characters. This is quiet annoying when using include command to reference another template store in utf-8 with bom encoding.

Browsers will also treat the invisible bom character as normal character, though they are invisible, this often breaks the style of web pages.


mitsuhiko commented May 20, 2013

All loaders accept a charset argument. Just change the default from utf-8 to utf-8-sig or fix your editor :)

@mitsuhiko mitsuhiko closed this May 20, 2013

MiCHiLU commented Sep 25, 2016 edited

I think should correspond by jinja2.

I am using jinja2 with compass about building AMP-HTML.
The AMP-HTML must include a style section without another CSS files.

My case is written style by SASS then compiling by compass.
The CSS file has BOM that is the specification of the compass.

Otherwise, we must continue to define a filter to remove the BOM, like this:

UTF8_BOM = bytearray([0xEF, 0XBB, 0XBF])

def strip_utf8_bom(s):
    if not isinstance(s, basestring):
        s = s.__str__()
    if s.startswith(UTF8_BOM):
        s = s[len(UTF8_BOM):]
    if not isinstance(s, unicode):
        s = s.decode('utf-8')
    return s

then use it:

{% import 'style.min.css' as style %}{{ style|strip_utf8_bom }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment