Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jinja blocks have a default parameter

Tags:

html

jinja2

Is there a way to create a default entry, if a block isn't filled?

I have a template with a button that says create.

myTemplate.html:

<a href="#" class="btn">create</a>

Up until now, all pages using this template have used the test create, however now I would like to be able to edit that text from my page.

Usually I would add a block to the template to be overridden by the page:

myTemplate.html:

<a href="#" class="btn">{% block createString %}{% endblock %}</a>

myPage.html:

{% extends "myTemplate.html" %}
{% block createString %}next{% endblock %}

However, for this I would like to have something like a default, so that if I don't have any createString block in the myPage, a default string will be used.

Something like:

myTemplate.html:

<a href="#" class="btn">{% block createString %}{% default %}create
                        {% endblock %}</a>

In the myPage.html the button will say next while in the other files extending myTemplate.html the button will read create.

like image 968
Markus Avatar asked Oct 16 '25 16:10

Markus


1 Answers

This was much simpler than I thought...

So in the template I just write the "default" code within the block.

myTemplate.html:

<a href="#" class="btn">{% block createString %}create{% endblock %}</a>

So in the pages I want the create text I simply ignore the createString block, while in the pages I want to change the text I override the template block like the following.

myPage.html:

{% extends "myTemplate.html" %}
{% block createString %}next{% endblock %}
like image 162
Markus Avatar answered Oct 19 '25 13:10

Markus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!