Multiline string in Django include statement

I am trying to be DRY with my Django templates, and have some code that I mix with CSS for a simple popup popup. I would like to reuse the code, but the contents of my popup will be HTML, which can span multiple lines. Is it possible to insert multi-line strings into a template variable?

I tried to do something funky with blocks and block.super, but this only works when expanding (not include)

Here is an example of what I would like to do. Is it possible?

index.html

 <body>
 <h2>My Popup</h2>
 {% include "snippets/popup.html" with class="p" spantext="Hover me" popupdiv="""
  <h2>This is a popup!</h2>
     <ul>
          <li>Something</li>
          <li>Something else</li>
     </ul>
 """
%}
 </body>

snippets/popup.html

 <div class="{{ class }}">
     <span class='pointer'>{{ spantext }}</span>
     <div class="popup">
         {{ popupdiv }}
     </div>
 </div>

I know that in Django it is impossible to have multi-line template tags, but is there any way around this other than crushing my entire html div into one line and avoiding quotes?

Greetings

+3
2

, "Parsing , " , . http://www.djangobook.com/en/2.0/chapter09.html

:

tags.py ( templatetags)

from django import template
from django.template.loader import get_template
from django.template.base import Node, TemplateSyntaxError

register = template.Library()

class PopupNode(Node):
    def __init__(self, nodelist, class_name, spantext):
        self.nodelist = nodelist
        self.class_name = class_name
        self.spantext = spantext

    def render(self, context):
        popup_html = get_template("ordersystem/snippets/popup.html")
        context.update({
            'class' : self.class_name,
            'spantext' : self.spantext,
            'popupdiv' : self.nodelist.render(context)
        })
        return popup_html.render(context)

@register.tag('popup')
def show_popup(parser, token):
    nodelist = parser.parse(('endpopup',))
    tokens = token.split_contents()
    if len(tokens) != 4:
        raise TemplateSyntaxError("show_popup is in the format 'popup with class=X spantext=Y")
    try:
        context_extras = [t.split("=")[1].strip('"') for t in tokens[2:]]
    except IndexError:
        raise TemplateSyntaxError("show_popup is in the format 'popup with class=X spantext=Y")
    parser.delete_first_token()
    return PopupNode(nodelist, *context_extras)

html :

{% popup with class_name=management spantext=Manage %}
<h2>This is a popup!</h2>
     <ul>
          <li>Something</li>
          <li>Something else</li>
     </ul>
{% endpoup %}
+2

- templatetags .

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

, , YourModule yourModule templatetags popup_tag.py

yourModule/
---- views.py
---- models.py
---- templates/
     ---- snippet/
          ---- popup.html
---- templatetags/
     ---- __init__.py
     ---- popup_tag.py

popup_tag.py :

from django import template

register = template.Library()

def show_pop_up(class, span_text, popupdiv):
    return {'class': class,
            'span_text': span_text,
            'pop_up_div': pop_up_div}

register.inclusion_tag('snippet/popup.html')(show_popup)

index.html.

{% load popup_tag %}

{% show_popup "class" "span_text" "popupdiv" %}
-1
source

All Articles