在Flask应用程序中使用Markdown混合图像 [英] Mix images with Markdown in a Flask app
问题描述
我正在使用 Flask-FlatPages
(和后续的 Frozen-Flask
)建立一个静态的站点。 / p>
在我的页面中,我想将文字与图像混合在一起。这将是这样做的天真的方式:
##看看*这个图像:
< img src ={{url_for('static',filename ='images / image.png')}}>
嗯,它不**似乎加载。
{{template tag}}
是不被解析,因为FlatPages通过降价运行页面,而不是通过Flask的模板系统(如果我没有弄错的话)。
如何获得正确的图像链接?
相关代码
#app.py
来自烧瓶导入Flask,render_template
from flask_flatpages import FlatPages
$ b $ app = Flask(__ name__)
pages = FlatPages(app)
@ app.route '/ tip /< path:path>')
def tip_detail(path):
tip = pages.get_or_404(path)
template = tip.meta.get('template', 'tip_detail.html')
return render_template(template,tip = tip)
和
#tip_detail.html
<!DOCTYPE html>
< html>
< head>
< title>< / title>
< / head>
< body>
< h1> {{tip.meta.title}}< / h1>
{{tip}}
< / body>
< / html>
解决了 https://github.com/naringas at https://github.com/SimonSapin/Flask-FlatPages/pull/1
事实证明,Flask -FlatPages不会呈现Jinja模板标签。但是,它可以选择设置自定义的HTML渲染器。
#将这些行添加到app.py
def prerender_jinja(text):
prerender_body = render_template_string(Markup(text))
return pygmented_markdown(prerendered_body)
app.config ['FLATPAGES_HTML_RENDERER'] = prerender_jinja
I am building a static site using Flask-FlatPages
(and following up with Frozen-Flask
).
Within my pages, I want to mix up the text with images. This would be the naive way to do this:
## Look at *this* image:
<img src="{{ url_for('static', filename='images/image.png') }}">
Hmm, it does **not** seem to load.
The {{ template tag }}
is not being parsed, because FlatPages runs the page through markdown and not through Flask's templating system (if I am not mistaken).
How do I go about getting the correct image link?
Relevant code
#app.py
from flask import Flask, render_template
from flask_flatpages import FlatPages
app = Flask(__name__)
pages = FlatPages(app)
@app.route('/tip/<path:path>')
def tip_detail(path):
tip = pages.get_or_404(path)
template = tip.meta.get('template', 'tip_detail.html')
return render_template(template, tip=tip)
and
#tip_detail.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>{{ tip.meta.title }}</h1>
{{ tip }}
</body>
</html>
Solved through a comment left by https://github.com/naringas at https://github.com/SimonSapin/Flask-FlatPages/pull/1
As it turns out Flask-FlatPages does not render Jinja template tags. It does however have an option to set a custom HTML renderer. Use that to first render the Jinja template before rendering the markdown.
#add these lines to app.py
def prerender_jinja(text):
prerendered_body = render_template_string(Markup(text))
return pygmented_markdown(prerendered_body)
app.config['FLATPAGES_HTML_RENDERER'] = prerender_jinja
这篇关于在Flask应用程序中使用Markdown混合图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!