jobiin
jobiin

Reputation: 479

Django include template, overwrite 'self' variable?

I've been tasked to fix a template structure in a Django app. I am not very familiar with Django as I deal mostly with front end projects built in React.

The Django app is using Wagtail as a backend CMS. I am looping through it's pages, and including a template block and trying to pass variables with the with statement on the include. I can successfully pass variables to the template block, but I need to overwrite the 'self' variable as the template block is looking for self.something.

Here is what my template looks like.

{% load static wagtailimages_tags wagtailcore_tags %}

<div class="c-card {% if self.variant %}c-card--{{ self.variant }}{% endif %}">
    {% if self.image %}
        {% image self.image original class="c-card__image" loading="lazy" %}
    {% endif %}

    {% if self.title %}
        <h3 class="c-card__title">{{ self.title }}</h3>
    {% endif %}

    {% if self.text %}
        <p class="c-card__content">{{ self.text }}</p>
    {% endif %}

    {% if self.button.title and self.button.url %}
        {% include './button_block.html' with button=self.button %}
    {% endif %}
</div>

and then there is the loop and include snippet...

{% for chapter_menu in page.chapters %}
    <div class="medium-6 large-4 columns">
        {{ chapter_menu.value.title }}
        {% include './blocks/card_block.html' with self=chapter_menu.value %}
    </div>
{%  endfor %}

Doing this self=chapter_menu.value results in a TypeError, which is expected as I am defining self twice. push() got multiple values for argument 'self'

How can I go about doing this? Any docs I can read besides the basic django templating docs? https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#with

Upvotes: 0

Views: 804

Answers (1)

jobiin
jobiin

Reputation: 479

add the only attribute to include.... Did not find this in any documentation...

{% include './blocks/card_block.html' with self=chapter_menu.value only %}

Upvotes: 1

Related Questions