Tags in included Templates are disregarding passed variables

Is this by design or am I doing something wrong? Here is the situation:

I have this in template_A:
{% if obj %}{% include template_B with thing=obj %}{% endif %}

and in template_B:

<div>{{ thing.name }}</div>
{% if thing.name == "text" %}<p> It is working!</p>{% endif %}

I find that thing.name evaluates in template_B and renders in the div but the paragraph block (<p></p>) does not render even when thing.name evaluates to "text". It appears passing objects between templates only work for {{ variables }} but not for {% tags %}

Is this the design?

I think what the questioner is trying to convey is that the include template is not working properly.

Here’s what you need to check:
. What is the HTML that appears in the final rendering?
. Are individual apps using templates with the same name?
. Is {{ obj.name }} valid in the template a?
. Aren’t you overwriting {{ thing }} in template b?

What you show should work, so it seems like something else is going wrong. Not very helpful, but just wanted to confirm that it should work as you expect.

1 Like

I think it should work, and you need to provide more information.
Try some debugging and provide the output:

  1. template_A.html.
{% if obj %}
    {% include "template_B.html" with thing=obj %}
{% endif %}
  1. template_B.html
<div>{{ thing.name }}</div>
<p>thing.name is: "{{ thing.name }}"</p> 
{% if thing.name == "text" %}
    <p>It is working!</p>
{% endif %}

1 Like

In addition to the excellent suggestion above, what is thing.name?
If thing.name is an object or a foreign key, it might render as “text”, but it is not == “text”. So knowing what these objects are is also important.

I was able to get this working. I could not replicate the faulty scenario. Some how, I fixed it without identifying the bug.