Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

django template tag on multiple line

I am creating a custom django template tag by using such a code :

@register.simple_tag(takes_context=True)
def render_listing(context, *args, **kwargs):
   ... my code ...

This works well, but in my template, it seems that all parameters must be on a single line, for example :

this works:

{% render_listing param1=val1 param2=val2 ... paramN=valN %}

but on multiple lines, it does not work :

{% render_listing param1=val1 
                  param2=val2 
                  ... 
                  paramN=valN %}

I tried multiple escape sequences but I did not succeeded,

Is there a way to specify a template tag on multiple lines ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, the Django template language does not support multiple line tags. See ticket 8652, which was closed as WONTFIX, or this thread from the django-developers mailing list.

Sometimes, if there is a repeated prefix, you can make it more readable by using the with tag. For example if you have,

{% render_listing param1=long.common.prefix.val1 param2=long.common.prefix.val2 param2=long.common.prefix.val3 %}

you could rewrite as

{% with prefix=long.common.prefix %}
{% render_listing param1=prefix.val1 param2=prefix.val2 param2=prefix.val3 %}
{% endwith %}

Often (but not always), a really long tag is an indication that you're putting too much logic in the template. See if you can move some of it into the view, model method, template tag or template filter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...