Home > Net >  Jinja2 HTML templates use multiple nested quotation marks (" and ')
Jinja2 HTML templates use multiple nested quotation marks (" and ')

Time:02-02

The question is simple, I am using jinja2 html templates and I am supposed to include {{ url_for('static', filename='img/background.png') }} inside of url(''), inside of style="".

The proble is that there is a quotation mark conflict, because there are two nested ' marks

<body id="body-pd" style="background: url('{{ url_for('static', filename='img/background.png') }}') no-repeat center center fixed; background-size: cover;">

Is there some sort of scape character to be used?

CodePudding user response:

The answer to this problem is also very simple: there's no problem. The Jinja environment (server side) and browser/frontend environment have different lifecycle.

Jinja only sees this part:

{{ url_for('static', filename='img/background.png') }}

It produces a path, e.g. /static/img/background.png and replaces the whole {{ ... }} snippet with it. The frontend environment never sees Jinja-related parts. So there's no quotation mark conflict, because half of them will be eliminated by Jinja on the server side.

  •  Tags:  
  • Related