# How can I pass a big html code block to a with variable in Django template?

**URL:** <https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685>\
**Category:** Templates & Frontend\
**Created:** [June 17, 2023, 6:16pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685 "2023-06-17T18:16:26Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nexgen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nexgen/32/14168_2.png) [@nexgen](https://forum.djangoproject.com/u/nexgen)\
**Post date:** [June 17, 2023, 6:16pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/1 "2023-06-17T18:16:26Z")

</div>

I’m looking to pass a big html code block value to a with variable. How can I achieve that elegantly?

Example:

```auto
{% include "includes/template.html" with info="big html code block" %}

```

I’m wondering how I can accomplish this elegantly. One option would be to escape all quotes and new lines. Is there any other better way to do that?

---

<div class="post-metadata">

**Author:** ![Rigo-Villalta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rigo-villalta/32/5667_2.png) [@Rigo-Villalta](https://forum.djangoproject.com/u/Rigo-Villalta)\
**Post date:** [June 18, 2023, 5:08pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/2 "2023-06-18T17:08:09Z")

</div>

I don’t think there is an elegant option to do that.  
Why don’t you try nested templates?

---

<div class="post-metadata">

**Author:** ![nexgen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nexgen/32/14168_2.png) [@nexgen](https://forum.djangoproject.com/u/nexgen)\
**Post date:** [June 18, 2023, 5:22pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/3 "2023-06-18T17:22:25Z")

</div>

Can you please given an example for that?

---

<div class="post-metadata">

**Author:** ![Rigo-Villalta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rigo-villalta/32/5667_2.png) [@Rigo-Villalta](https://forum.djangoproject.com/u/Rigo-Villalta)\
**Post date:** [June 18, 2023, 8:10pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/4 "2023-06-18T20:10:21Z")

</div>

In your case I thing it should be something like:

```auto
{% extends "base.html" %}
  {% block content %}
    {% if something %}
      {% include "includes/template1.html" %}
    {% elif other:thing %}
      {% include "includes/template1.html" %}
    {% endif %}
  {% endblock content %}

```

And you complement the HTML in each template.

I write this directly on the browser, so be careful with typos.

---

<div class="post-metadata">

**Author:** ![nexgen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nexgen/32/14168_2.png) [@nexgen](https://forum.djangoproject.com/u/nexgen)\
**Post date:** [June 19, 2023, 4:01am UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/5 "2023-06-19T04:01:49Z")

</div>

Here in include you are passing same template1.html. Did you mean to pass template1 or template2 depending upon the condition?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [June 19, 2023, 10:56am UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/6 "2023-06-19T10:56:30Z")

</div>

Yes, you can include any template you want.

---

<div class="post-metadata">

**Author:** ![Rigo-Villalta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rigo-villalta/32/5667_2.png) [@Rigo-Villalta](https://forum.djangoproject.com/u/Rigo-Villalta)\
**Post date:** [June 19, 2023, 3:37pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/7 "2023-06-19T15:37:46Z")

</div>

As @leandrodesouzadev wrote, you can pass any template you want. I meant 2, but I made a mistake.

---

<div class="post-metadata">

**Author:** ![nexgen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nexgen/32/14168_2.png) [@nexgen](https://forum.djangoproject.com/u/nexgen)\
**Post date:** [June 20, 2023, 5:33am UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/8 "2023-06-20T05:33:34Z")

</div>

Thank you for the suggestions.

However, I’m trying to use the includes similar to function.

Here is what I have in my includes:

```auto
<!--begin::Row-->
<tr>
    <td class="text-gray-400 min-w-175px w-200px">{{ detail_title }}</td>
    <td class="text-gray-800 min-w-200px">
        {{ detail_info }}
    </td>
</tr>
<!--end::Row-->

```

It makes it much easier to include the snippet without thinking about all the html codes and I can just supply the variable I need.

The problem I’m facing is when the detail\_info can be multi line html codes.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [June 20, 2023, 11:16am UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/9 "2023-06-20T11:16:44Z")

</div>

Then i think that what you want is to create a custom template tag.  
Read [this entire section](https://docs.djangoproject.com/en/4.2/howto/custom-template-tags/#writing-custom-template-tags) of the documentation. You probably want to create a inclusion\_tag from what i understand.

---

<div class="post-metadata">

**Author:** ![nexgen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nexgen/32/14168_2.png) [@nexgen](https://forum.djangoproject.com/u/nexgen)\
**Post date:** [June 20, 2023, 11:49am UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/10 "2023-06-20T11:49:40Z")

</div>

Even if I use the inclusion tag, I still need to pass a variable to it and we are back to same problem.

How can I create a variable which have multiline html text?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 20, 2023, 11:57am UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/11 "2023-06-20T11:57:22Z")

</div>

I’m not sure I understand why you need to create a variable here in the template - aren’t you creating that variable in your view, and passing it to the rendering engine in the context?

---

<div class="post-metadata">

**Author:** ![nexgen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nexgen/32/14168_2.png) [@nexgen](https://forum.djangoproject.com/u/nexgen)\
**Post date:** [June 20, 2023, 12:28pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/12 "2023-06-20T12:28:08Z")

</div>

I already pass the data from view. For example I use it like this:

```auto
{% include "includes/template.html" with detail_title=data.title detail_info=data.info %}

```

However, depending upon the data the detail\_info can be any of these:

```auto
detail_info = {{data.info}}

```

```auto
detail_info = <a href="{% url 'my-url' %}" target="_blank">{{data.info}}</a>

```

```auto
detail_info = <div class="abc def"><a href="{% url 'my-url' %}" target="_blank">{{data.info}}</a><button>Some Button</button></div>

```

As you can see I’ll be passing the formatting code as well for the detail\_info since it is dynamic and I need to decide that while editing the template.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 20, 2023, 12:45pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/13 "2023-06-20T12:45:04Z")

</div>

Personally, I wouldn’t organize it that way. (I’m still not sure I fully understand the situation - I’m sure these are simplified examples from what’s a more complex environment. And without having a clearer and more complete picture, I’m not sure that anything I could say here would satisfy all your core requirements.)

In the general case, I’d have my `if` tags in the included template, to determine which of the three “wrappers” around `{{ data.info }}` to use - but to handle that selection of formatting within the template.

What I might suggest is that you take a look at how Django itself uses templates for things like forms to see how they handle the various options available, and all the conditional template processing that occurs. It might give you some ideas for a different/cleaner approach.

Also be aware that you can include templates within an included template.

---

<div class="post-metadata">

**Author:** ![nexgen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nexgen/32/14168_2.png) [@nexgen](https://forum.djangoproject.com/u/nexgen)\
**Post date:** [June 20, 2023, 1:04pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/14 "2023-06-20T13:04:39Z")

</div>

My question can be summed up into this one thing:

I can define a variable inside the template like this:  
{% with var=“value”}

However what if the value is not such a simple word, phrase or sentence and it instead contain multi-line description.

Is there anyway to define such variable in django template?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 20, 2023, 1:41pm UTC](https://forum.djangoproject.com/t/how-can-i-pass-a-big-html-code-block-to-a-with-variable-in-django-template/21685/15 "2023-06-20T13:41:56Z")

</div>

As literal text, no. Tags may not span lines.
