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

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


{% 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?

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

Can you please given an example for that?

In your case I thing it should be something like:

{% 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.

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

Yes, you can include any template you want.

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

Thank you for the suggestions.

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

Here is what I have in my includes:

    <td class="text-gray-400 min-w-175px w-200px">{{ detail_title }}</td>
    <td class="text-gray-800 min-w-200px">
        {{ detail_info }}

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.

Then i think that what you want is to create a custom template tag.
Read this entire section of the documentation. You probably want to create a inclusion_tag from what i understand.

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?

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?

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

{% include "includes/template.html" with detail_title=data.title %}

However, depending upon the data the detail_info can be any of these:

detail_info = {{}}
detail_info = <a href="{% url 'my-url' %}" target="_blank">{{}}</a>
detail_info = <div class="abc def"><a href="{% url 'my-url' %}" target="_blank">{{}}</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.

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 {{ }} 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.

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?

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