1

I have a function to automatically replace issue IDs to links:

def addlinks(text):
    from flask import Markup
    idlist = getallissueids()
    for issueid in idlist:
        text = text.replace( issueid, "<a href=/browse/" + issueid +">" + issueid + "</a>")
    return Markup(text)

Then {{text}} is passed to template.

It works, but there is a side effect - all kind of html is unescaped in text after this. Is there any way to avoid html escaping only for the link?

2 Answers 2

2

use the safe filter on text. it would look like this in the template,

{{ text |safe }}

safe will state that the html found in this text is safe to render and as such doesn't have to be escaped.

the flask page on templates briefly talks about the safe filter around half way down the tutorial. The safe filter just says that everything in this text is safe and turns off html escaping.

Sign up to request clarification or add additional context in comments.

Comments

0

That's the workaroung I found during this night:

def addlinks(text):
    from flask import Markup
    idlist = getallissueids()
    for issueid in idlist:
        parts = text.split(issueid)
        text = Markup("<a href=/browse/" + issueid +">" + issueid + "</a>").join(parts)     
    return text

Could you please explain how could I do the same thing using this "safe" feature?

1 Comment

I've updated my answer. The basic idea is that the safe keyword turns off auto-escaping of html found in jinja2 templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.