Flask-Bootstrap does not have a footer
block by default. The way of implementing one is covered in the FAQ
but I think this is fairly misleading.
From the example given, it is suggested that a new template should be created as base-with-footer.html
, like:
{% extends "bootstrap/base.html" %}
{%- block content %}
{{super()}}
{%- block footer %}
<footer>© 2016 Awesome, Inc.</footer>
{%- endblock footer %}
{%- endblock content %}
Any time you want to extend from the base-with-footer.html
, you'll have to call super()
inside the content
block. This is ugly and beats DRY.
I suggest a different approach: use footer
outside the content
block.
For the template in my_custom_blueprint/my_base.html
{% extends "bootstrap/base.html" %}
{# stuff happens here, like overrides of the original template in https://github.com/mbr/flask-bootstrap/blob/master/flask_bootstrap/templates/bootstrap/base.html #}
{% block content %}<p>Hello World!</p>{%- endblock content %}
{% block footer %}{%- endblock footer %}
Now in my_base_with_footer.html
{% extends "my_custom_blueprint/my_base.html" %}
{% block footer %}<p>Here goes your footer!</p>{%- endblock footer %}
With this approach you can reuse any of the blocks from the base without having to call super()
each time you need to write in contents
. You just have to extend from the template with the footer.
Thanks for this Diego, I was also struggling to include a footer in an elegant manner, your solution was the only one that made any sense however I could not get it to work to my satisfaction. I ended up building on your idea and doing something slightly different.
I decided not to bring the 'content' block into my rendered pages, but just leave it in the base.html template.
I then created a new 'content' block called 'main' and nested it inside the original 'content' block along with the footer block.
my-base.html
and then within the rendered pages all I need to do is override or extend the 'main' block.
my-page.html
There is need to include the footer block again as we are not overriding it or extending it. Once it's defined within our base template we're done with it unless we want to change it. base.html is acting as a proper template and the individual rendered pages are acting as overrides. I'm not repeating or recycling any blocks, all of which I find acceptable.
I nested the 'main' and 'footer' blocks within the 'content' block so that they would render correctly. This was an issue raised elsewhere when trying to create a custom 'footer' block. I'm essentially treating the 'content' block the same way I would the
<body>
tag. It encapsulates all content and so is not something that needs to be repeated in individual template files. This recycling and repeating of code seems to be something that is missed on all of the tutorials online, where all of the code is recycled. Your solution was the only one to address this but it would not work on my machine, but at least it inspired me to come up with another solution.Thank you for sharing. 👍
/DM