Multiple blocks of same name in Jinja2

In Jinja2, I have a base template like this:

<title>{% block title %}{% endblock %} - example.com</title>
[...]

<h1> 
  {% block title %}{% endblock %} - example.com
</h1>

Jinja2, then, fails with the following message:

  lines = [self.message, '  ' + location]
: block 'title' defined twice

It must be now evident as to what I am trying to do – to have the same title in two places: the TITLE tag and the H1 tag, but the part of the title is actually provided by other derived templates.

How does one typically achieve this?

multiple arbitrary blocks in Jinja2 macros

I use both Jinja2 and Nunjucks (depending on the project), but have yet to figure out how to create reusable elements with multiple blocks containing arbitrary HTML. For example (pseudo-code): {% macr

Jinja2 multiple variables in same for loop

I would like to populate the same section of a Jinja2 for loop with data from 2 separate SQL queries using Python / Webapp2 / Jinja2. Specifically, I am storing team info in a variable called team a

Jinja2 Environment option “lstrip_blocks” produces TypeError

I am using Ubuntu 12.04, python 3.2.3 and jinja2 2.6-1. When passing the option lstrip_blocks to the Environment as follows, I get this error **TypeError: /__init__() got an unexpected keyword argum

Jinja2 extension multiple keyword arguments

I can not find how to pass multiple named arguments to jinja2 Extension. I want something like that: {% some_extension foo=’foo’ bar=’bar’ %} data {% end_someextension %}

Parameterized reusable blocks with Jinja2 (Flask) templating engine

In Jinja2 templating engine (using Flask), I want to achieve something like that: {% reusable_block avatar(user) %} <img src='{{ user.avatar }}’ title='{{ user.name }}’/> {% reusable_block %} a

Rendering jinja2 blocks as json

I’ve created a jinja2 template that should support both standalone rendering and ajax rendering. The template looks like: {% if is_ajax %}{% extends ‘ajax.html’ %}{% else %}{% extends ‘site.html’ %}{%

wrapping blocks in jinja2

I have two jinja2 templates where the second extends the first one: <h1>Some title</h1> {% block content %} {% endblock %} and {% block content %} content {% endblock %} Now I want to be

concat multiple block in jinja2?

I use jinja2 for my template engine in python. i would like to join content of multiple block and would like to render it at the end of the template, just before tag. { they are various JavaScript sn

Getting a dict of all the blocks in a Jinja2 template

Let’s say I have some Jinja2 template with several blocks in it: {% block A %}Blah{% endblock %} {% block B %}whatever{% endblock %} {% block C %}you get the idea{% endblock %} I want a Python functi

Multiple renders of jinja2 templates?

Is there any way to do this with jinja2? template = Template({{ var1 }}{{ var2 }}) rendered1 = template.render(var1=5) # 5-{{ var2 }} rendered2 = Template(rendered1).render(var2=6) # 5-6 basica

Answers

As documented here, defining a block creates a macro with the name of the block in the special “self” object:

<title>{% block title %}{% endblock %} - example.com</title>
[...]

<h1> 
  {{ self.title() }} - example.com
</h1>