Create a gist now

Instantly share code, notes, and snippets.

Embed
different delimiters in jinja2 + flask
from flask import Flask, render_template_string, request
class CustomFlask(Flask):
jinja_options = Flask.jinja_options.copy()
jinja_options.update(dict(
block_start_string='<%',
block_end_string='%>',
variable_start_string='%%',
variable_end_string='%%',
comment_start_string='<#',
comment_end_string='#>',
))
app = CustomFlask(__name__)
app.config['DEBUG'] = True
@app.route("/")
def index():
template = """
<# this is a jinja2 comment #>
<% block stuff %>
<h1>Jinja2</h1>
<% for i in range(5) %>
<p>Hello %% name %%!</p>
<% endfor %>
<h1>Mustache</h1>
<p>{{something}}</p>
{{#items}}
{{#first}}
<li><strong>{{name}}</strong></li>
{{/first}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/items}}
<% endblock %>
"""
return render_template_string(template, name=request.values.get('name', 'world'))
if __name__ == "__main__":
app.run(use_debugger=True, use_reloader=True)
@darkdarkfruit

This comment has been minimized.

Show comment
Hide comment
@darkdarkfruit

darkdarkfruit Oct 29, 2012

very nice

very nice

@howardhamilton

This comment has been minimized.

Show comment
Hide comment
@howardhamilton

howardhamilton May 10, 2013

Like it! Nice way of getting variable delimiters in different frameworks to play with each other.

Like it! Nice way of getting variable delimiters in different frameworks to play with each other.

@misaelnieto

This comment has been minimized.

Show comment
Hide comment
@misaelnieto

misaelnieto Oct 14, 2013

¡Beautiful! Thanks

¡Beautiful! Thanks

@horacioibrahim

This comment has been minimized.

Show comment
Hide comment
@horacioibrahim

horacioibrahim Mar 29, 2015

Great!

Great!

@g8gg

This comment has been minimized.

Show comment
Hide comment
@g8gg

g8gg Jul 31, 2017

Great!

g8gg commented Jul 31, 2017

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment