0
votes

I am moving my blog over from Jekyll to Flask. In my Jekyll blog I use Liquid includes to process overhead heavy items like youtube videos and images and to ensure uniformity so that they can all be updated in the same place if there's a style or code change to the site.

In Flask is there a way to have the Jinja2 template process Liquid variables in the body of the blog article when it's passed to render_template()?

Example:

@route('/article')
def article():
    blog_body = "<h1>My Video</h1>{% include '_media.html' type='youtube' id='a2343lj' %}"
    return render_template('home.html.j2', content = blog_body)

And then in home.html.j2:

{% block page_content %}
{{ content }}
{% endblock %}

When testing, the template renders the literal string and does not process the include in the variable. Is there any way to get the Jinja template to render the Liquid syntax in the variable passed to it?

1

1 Answers

0
votes

While there does not appear to be any way to render a template string using Jinja2 syntax, there is a workaround. At the time the variable is set, you can process it using render_template_string from Flask.

Example:

from flask import render_template, render_template_string

@route('/article')
def article():
    blog_body = render_template_string("<h1>My Video</h1>{% include '_media.html' type='youtube' id='a2343lj' %}")
    return render_template('home.html.j2', content = blog_body)

Then at least the variable will be rendered at the time it is set, even if not whenever the template is rendered.