Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a block optional in Django template

In Django's templates system, if I have a block that I want to make optional using an if statement, how do I do it?

I was trying this:

{% if val %}{% block title %}Archive {{ foo }}{% endblock %}{% endif %}

But that doesn't work. Is there a way to do that, so that for a given value (in this case Null) the block isn't issued and the base template uses the original values?

Edit: Let me be a little more specific, so that it is easier to answer.

I have a page with 10 entries per page. The user can then go on to the next page and see the next ten items. For each further page they go, past the first, I would like to have the title tag say something like "Archive 1" or "Archive 10" but if they get back to the original page, it is no longer archive, and it should just go to the original site title already given in the base templates.

like image 497
Vernon Avatar asked Aug 11 '10 13:08

Vernon


People also ask

Which option does Django templates accept?

DjangoTemplates engines accept the following OPTIONS : 'autoescape' : a boolean that controls whether HTML autoescaping is enabled. It defaults to True . Only set it to False if you're rendering non-HTML templates!

What is block in Django template?

Definition and Usage In master templates the block tag is a placeholder that will be replaced by a block in a child template with the same name. In child templates the block tag is content that will replace the placeholder in the master template with the same name.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What is Forloop counter in Django?

Django for loop counter All the variables related to the counter are listed below. forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.


2 Answers

I ran into a similar issue with a project I'm working on. Here's how I resolved it using {{ block.super }} to pull the default value from the parent block:

My parent template contains:

{% block title %}Default Title{% endblock %}

My child template contains:

{% block title %}
    {% if new_title %}{{ new_title }}{% else %}{{ block.super }}{% endif %}
{% endblock %}

*Note: You may want to wrap the code in {% spaceless %}{% endspaceless %} if you plan to use the result in an HTML title tag.

(It looks like Jordan Reiter posted the same solution in the comments of the original question a bit before my response.)

like image 171
Jamie Avatar answered Oct 19 '22 19:10

Jamie


As far as I understand blocks are placeholders to be "overridden" in the child templates. They have to be defined at "compile time" and not at "run time".

As for your specific problem why not modify the title based on the page number (assuming you use pagination)? Something like this:

{% block title %}
    {% ifequal page 1 %}Current{% else %}Archive {{ page }}{% endifequal %}
{% endblock %}
like image 23
Manoj Govindan Avatar answered Oct 19 '22 18:10

Manoj Govindan