代码之家  ›  专栏  ›  技术社区  ›  Patrick

Django“include”语句中的多行字符串

  •  4
  • Patrick  · 技术社区  · 12 年前

    我正在尝试使用Django模板,并将一些代码与CSS混合,以实现简单的悬停在弹出窗口上。我想重用代码,但弹出窗口的内容将是HTML,可能跨越多行。是否可以将多行字符串填充到模板变量中?

    我试着用积木和 block.super 但这似乎只在扩展时起作用(不是 include )

    这是我想做的一个例子。有可能吗?

    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>
    

    我知道Django中不可能有多行模板标记,但除了将我的所有div html压缩到一行,并转义任何引号之外,还有什么方法可以解决这个问题吗?

    干杯

    2 回复  |  直到 12 年前
        1
  •  3
  •   Patrick    12 年前

    结果发现“解析到另一个模板标记”是我想要的。 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
  •  -1
  •   pepourquier    12 年前

    最好的方法应该是在模块中使用 包含标签 .

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

    因此,假设您是YourModule YourModule,其中包含文件夹模板和文件 弹出标记.py

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

    你的 弹出标记.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)
    

    然后,您只需调用模板中的标记 索引html .

    {% load popup_tag %}
    
    {% show_popup "class" "span_text" "popupdiv" %}