Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django template tag on multiple line

Tags:

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 ?

like image 755
Eric Avatar asked Mar 05 '18 11:03

Eric


People also ask

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What are template tags in Django?

The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client. In the next chapters you will learn about the most common template tags.

What does the built in Django template tag spaceless do?

spaceless. Removes whitespace between HTML tags. This includes tab characters and newlines.

What is Forloop counter in Django?

A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.


2 Answers

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.

like image 141
Alasdair Avatar answered Oct 21 '22 02:10

Alasdair


It's pretty straightforward to enable, though hackish:

import re from django.template import base base.tag_re = re.compile(base.tag_re.pattern, re.DOTALL) 

"Using" it is simple; one place I find it especially useful is {% include %} tags:

{% include 'my/sweet/modal-template.template' with     message="Hey, do you really want to frob the widget?"     yes="Heck yes I do!"     no="No frickin' way!"     icon="error" %} 

I haven't tested this in more recent versions of Django but I imagine it could be adapted; that worked at least back around 1.8. I should point out that in theory some tags that do custom parsing of their arguments could break; in practice I haven't had any trouble in the last ~10 years of Django programming.

Ref: http://zachsnow.com/blog/2016/multiline-template-tags-django/

like image 38
Zach Snow Avatar answered Oct 21 '22 04:10

Zach Snow