0
我無法從我的模板縮進文本。我在我的mainTemplate.html中使用多個 
縮進文本的開頭,但是,如果文本繼續到新行,那麼不會縮進新行。所以,我一直在嘗試使用過濾器來正確縮進每一行。我的模板使用{% include %}
標記來呈現幷包含我嘗試縮進的文本的子模板(display_citations.html)。是否有可能將呈現的子模板存儲爲字符串,以便將其傳遞給我的「縮進」過濾器?如果沒有,是否有另一種縮進該子模板內容的方法?使用django過濾器縮進模板
mainTemplate.html
{% for publication in value %}
{% include "software/display_citations.html" with publication=publication url="/select_citations/"%}
{% endfor %}
display_citations.html
{{ publication.authors }}. {{ publication.title }}{% if not publication.title_ends_with_punct %}.{% endif %}{% if publication.journal %} {{ publication.journal }}.{% endif %}{% if publication.month %} {{ publication.month_long }}{% endif %} {{ publication.year }}.{% if publication.volume %}{% if publication.number %} {{ publication.volume }}({{ publication.number }}){% else %} {{publication.volume}}{% endif %}{% endif %}{% if publication.pages %}: {{ publication.pages }}.{% else %}.{% endif %}
<br><a href="{{url}}{{ publication.pk }}/?ris" target="_blank" download="Citations.ris">RIS</a>
views.py
register = template.Library()
@register.filter(name='indent')
@stringfilter
def indent(value, arg=1):
import re
regex = re.compile("^", re.M)
return re.sub(regex, "\t"*int(arg), value)
我想我需要在mainTemplate.html做的就是以某種方式創建一個字符串從我的子模板中,然後傳遞給我的過濾器......?像這樣的事情也許:
text_to_be_indented = {% include "software/display_citations.html" with publication=publication url="/select_citations/"%}
{{ text_to_be_indented | indent:"4" }}
哦,天哪!這太容易了 - 非常感謝你! – steph 2014-12-03 02:39:47