templates tag   0   1575
5 lesser used Django template tags


We already know how to use for, if-else, filter and url template tags in Django. We can also create custom template tags in Django if any requirement is not getting fulfilled with existing tags. 


Here we are introducing you with 5 Django template tags which are lesser-known and used by beginner Django developers.


1. lorem

This template tag is used to insert random lorem ipsum Latin text in the template. This is useful when you want to show the sample data instead of blank space.

This tag accepts 3 optional parameters: count, method, and random.

The count is the number of words or paragraphs to produce. The method can have values w for words, p for HTML paragraphs and b for plain-text paragraphs. When the third parameter, random, is used, the text is generated randomly instead of using lorem ipsum text.

Refer to this Django template fiddle for example and demo.


2. templatetag

Double curly braces are used to display variables in Django templates. What if you want to display just curly braces in template. For this to achieve we can use templatetag template tag.


3. cycle

This tag accepts the variable number of arguments and outputs the next argument each time this tag is called. Once all arguments have been called, loop restarts from the starting.

We can use variables or string or a mix of both as arguments for this tag.

Refer to this Django template fiddle for example and demo. 

<!-- mylist is {"mylist":["Line 1","Line 2","Line 3","Line 4","Line 5","Line 6"]} -->
{% for item in mylist %}
    <div style="color:{% cycle 'red' 'green' 'blue' 'yellow' %}">
        {{item}}
    </div>
{% endfor %}


Output:

cycle tag in django template tags


4. firstof

This template tag accepts the variable number of arguments and returns the first argument which is not False i.e. which is not zero, empty string or False

Refer to this Django template fiddle for example and demo. 


5. phone2numeric

Converts any phone number to the numeric equivalent even if the phone number is not valid.

It will not convert any integer or boolean values.

Refer to this Django template fiddle for example and demo. 



templates tag   0   1575

Related Articles:
Django Template Fiddle Launched !!!!
Django template fiddle, Experimenting with Django templates, Playing with Django template, Django fiddle, template fiddle, djangotemplatefiddle.com,...
Creating custom template tags in Django
Custom template tags in Django, creating new template tags in Django, Step by step guide to create and use custom template tags in Django, how to create custom template tags in Django, how to use custom template tag in Django,...
Solving Django Error: TemplateDoesNotExist at /app_name/
How to solve TemplateDoesNotExist error in Django projects, reason for TemplateDoesNotExist error in Django, fixing TemplateDoesNotExist error in Django...
For loop in Django template
using for loop in Django templates, Using break in Django template for loop, Using range in django template for loop, How to access index in for loop in django template, for - empty in django template...

SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.


Recent Posts:






© 2017-2019 Python Circle   Contact Us   Advertise with Us