Django模板标记在多行 [英] django template tag on multiple line

查看:77
本文介绍了Django模板标记在多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码创建自定义Django模板标签:

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 :

这可行:

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

但在多行中不起作用:

{% 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 ?

推荐答案

否,Django模板语言不支持多个行标签。请参阅票8652 (已作为WONTFIX关闭)或。google.com/ d / msg / django-developers / wRKgnMIhl6g / A7MdArOlufwJ rel = noreferrer>此线程

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.

有时,如果前缀重复,则可以使用with标记使其更具可读性。例如,如果您有

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 %}

您可以重写为

{% 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.

这篇关于Django模板标记在多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆