2014-02-16 30 views
3

我想要與我的Django模板一起幹,並且有一些代碼可以與CSS混合以實現簡單的懸停彈出窗口。我想重複使用代碼,但彈出窗口的內容將是HTML,可能跨越多行。是否可以將多行字符串填充到模板變量中?在Django'包含'語句中的多行字符串

我試着做一些時髦的積木和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

事實證明,「解析,直到另一個模板標籤」是我所追求的。 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 %} 
-1

,最好的辦法應該是創造你的模塊中創建一個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" %} 
+0

我沒有測試過這一點,但我的猜測是,由於您仍在使用模板標籤('show_popup'),您仍然無法添加跨越多行的文本?我已經修改了我的問題,以更明確地表明我想要的內容 – Patrick

+0

這不起作用。它仍然在多條線路上短缺。 – Patrick

+0

你想絕對使用一個html列表嗎?您是否可以在調用模板標籤時使用循環變量。像{%show_popup「class」,「span_text」變量%}和模板{%變量%中的項%}

  • {%endfor%} – pepourquier

    相關問題