torchbox / django-pattern-library

UI pattern libraries for Django templates
https://torchbox.github.io/django-pattern-library/
BSD 3-Clause "New" or "Revised" License
360 stars 44 forks source link

Issue with nested templates and child templates with overridden tags that include another template #209

Open helenb opened 1 year ago

helenb commented 1 year ago

Found a bug? Please fill out the sections below. 👍

Issue Summary

This issue is present in both 0.7.0 and 1.0.0 versions, but not in 0.6.0

You can repeat the issue by creating a simple template called test_block.html that includes another template in a for loop using include_block

test_block.html:

{% load wagtailcore_tags %}

{% for button in value %}
    {% include_block button %}
{% endfor %}

test_block.yaml:

context:
  value:
    - button
    - button

tags:
  include_block:
    'button':
      template_name: 'patterns/molecules/streamfield/blocks/cta_button_block.html'

test_block.html will render by itself in the pattern library without any issue. However, if we have a stream_block.html template that is being used to include some example blocks for a page.body then we see the bug.

Take a page template that includes the following markup:

{% include_block page.body %}

This will repeatedly use a stream_block template to render the blocks in page.body. The yaml for the stream_block instructs the pattern library to render another template story_container.html, which is only used by the pattern library, and includes a list of example blocks to be rendered for page templates. If we add our test_block.html to this like so it renders an error:

story_container.html:

{% include "patterns/molecules/streamfield/blocks/paragraph_block.html" %}
{% include "patterns/molecules/streamfield/blocks/image_block.html" %}
{% include "patterns/molecules/streamfield/blocks/quote_block.html" %}
{% include "patterns/molecules/streamfield/blocks/cta_button_block.html" %}
{% include "patterns/molecules/streamfield/blocks/document_block.html" %}
{% include "patterns/molecules/streamfield/blocks/test_block.html" %}

The error is as follows:

TypeError at /pattern-library/render-pattern/patterns/pages/standardpages/information_page.html
sequence item 1: expected str instance, NoneType found
Request Method: GET
Request URL:    http://localhost:8000/pattern-library/render-pattern/patterns/pages/standardpages/information_page.html
Django Version: 3.2.16
Exception Type: TypeError
Exception Value:    
sequence item 1: expected str instance, NoneType found
Exception Location: /venv/lib/python3.8/site-packages/django/template/defaulttags.py, line 221, in render
Python Executable:  /venv/bin/python
Python Version: 3.8.16
Python Path:    
['/app',
 '/app',
 '/usr/local/lib/python38.zip',
 '/usr/local/lib/python3.8',
 '/usr/local/lib/python3.8/lib-dynload',
 '/venv/lib/python3.8/site-packages']
Server time:    Fri, 09 Dec 2022 11:14:38 +0000

If I change the include_block tag in test_block.html to a simple include tag and pass it the template name, then the issue no longer occurs. If I change the pattern library version for my project to 0.6.0 the issue no longer occurs.

Steps to Reproduce

Create a build using pattern library version 0.7.0 or higher Create a test template as described above, with a forloop and an include_block Try adding the test template to a story_container type template, being used to render a set of example blocks for page.body in the pattern library.

Technical details

b-ggs commented 1 year ago

I've reproduced this in a demo project here: https://github.com/b-ggs/django-pattern-library-issue-209

Seems that the issue happens with these specific conditions:

Templates B and C render properly, but as of django-pattern-library 0.7.0, Template A will not render.

helenb commented 1 year ago

I've updated the title to reflect @b-ggs's findings