The doc says about @register.filter below:
Custom filters are Python functions that take one or two arguments:
- The value of the variable (input) – not necessarily a string.
- The value of the argument – this can have a default value, or be left out altogether.
And, the doc says about @register.simple_tag below:
This function, which is a method of django.template.Library, takes a function that accepts any number of arguments, wraps it in a render function and the other necessary bits mentioned above and registers it with the template system.
And, the doc only says about @register.tag below:
Finally, register the tag with your module’s Library instance, as explained in writing custom template tags above.
And, the doc says about @register.inclusion_tag below:
Another common type of template tag is the type that displays some data by rendering another template. For example, Django’s admin interface uses custom template tags to display the buttons along the bottom of the “add/change” form pages. Those buttons always look the same, but the link targets change depending on the object being edited – so they’re a perfect case for using a small template that is filled with details from the current object.
But, I don't understand what they are like so what is the difference between @register.filter @register.simple_tag, @register.tag and @register.inclusion_tag in Django Templates?
@register.filter:
takes_contextparameter.asargument.@register.simple_tag:
takes_contextparameter.contextas the 1st parameter iftakes_context=Trueis set to@register.simple_tagotherwise there is an error. *The 1st parameter name must becontextotherwise there is an error.asargument.@register.tag:
strtype value from aNodeclass based object to Django Templates otherwise there is an error.takes_contextparameter.parseras the 1st parameter andtokenas the 2nd parameter in convention (other names are fine) otherwise there is an error.asargument.@register.inclusion_tag:
takes_contextparameter.contextas the 1st parameter iftakes_context=Trueis set to@register.inclusion_tagotherwise there is an error. *The 1st parameter name must becontextotherwise there is an error.asargument.<@register.filter>
For example, return a dictionary to
index.htmlfromperson_view()as shown below:Then, pass
nametoperson_filter()as shown below:Then, return
namefromperson_filter()toindex.htmlas shown below:Then, this below is displayed:
Next, pass
nameandagetoperson_filter()as shown below:Then, return
nameandagefromperson_filter()toindex.htmlas shown below:Then, this below is displayed:
<@register.simple_tag>
For example, return a dictionary to
index.htmlfromperson_view()as shown below:Then, pass
name,ageand"Good Person"toperson_tag()as shown below:Then, return
name,ageandextra_infofromperson_tag()toindex.htmlas shown below:Then, this below is displayed:
Next, set
takes_context=Trueto@register.simple_tag, then forperson_tag(), we don't neednameandageparameters, then must putcontextwhich is a dictionary as the 1st parameter as shown below. *If the 1st parameter name is notcontext, there is an error:Then, pass only
"Good Person"toperson_tag()as shown below:Then, this below is displayed:
Actually, you can still pass
nameandagefromindex.htmltoperson_tag()as shown below:But,
person_tag()needs 2 more parameters as shown below. *Other names are fine fornameandageparameters:Next, store the return values from
person_tag()toperson_infowithasargument, then display it as shown below. *The tag withasargument is not displayed:Then, this below is displayed:
Lastly in
person_tag(), setcontext['extra_info'], then return an empty string toindex.htmlas shown below:Then, display
name,ageandextra_infoas shown below:Then, this below is displayed:
<@register.tag>
For example, return a dictionary to
index.htmlfromperson_view()as shown below:Then, pass
name,ageand"Good Person"toperson_tag()as shown below:Then from
person_tag(), returnPersonNode()which returns 4 tokens fromrender()toindex.htmlas shown below. *The 1st token is the tag namepersonand the 2nd token isnameand the 3rd token isageand the 4th token is"Good Person"and@register.tagcannot havetakes_contextparameter and@register.tag's function must haveparseras the 1st parameter andtokenas the 2nd parameter in convention (other names are fine) otherwise there is an error:Then, this below is displayed:
And, this code below returns the 1st token, the actual
nameandagevalues got by the 2nd and 3rd tokens respectively and the 4th token without double quotes. *template.Variable("key.dot.notation").resolve(dict)can get the value from a dictionary with a key dot notation and you can see my answer explainingtemplate.Variable("key.dot.notation").resolve(dict):Then, this below is displayed:
And, this code below returns
nameandagevalues fromcontextandGood Person:Then, this below is displayed:
And in
render(), setcontext['extra_info'], then return an empty string toindex.htmlas shown below:Then, display
name,ageandextra_infoas shown below:Then, this below is displayed:
In addition, we can create a comment tag to comment words with
parserparameter and@register.tagas shown below:Then, surround the words with the comment tag
{% comm %}and{% endcomm %}not to display the words as shown below:<@register.inclusion_tag>
For example, return a dictionary to
index.htmlfromperson_view()as shown below:Then, pass
name,ageand"Good Person"toperson_tag()as shown below:Then, return a dictionary from
person_tag()toresult.htmlas shown below:Then, display
n,aandeiinresult.htmlas shown below:Then, this below is displayed:
Be careful,
name,ageandextra_infobelow cannot be displayed inresult.htmlbecause they are not returned fromperson_tag()toresult.html:Next, set
takes_context=Trueto@register.inclusion_tag, then forperson_tag(), we don't neednameandageparameters, then must putcontextwhich is a dictionary as the 1st parameter, then setcontext['extra_info']as shown below. *If the 1st parameter name is notcontext, there is an error:Then, pass only
"Good Person"toperson_tag()as shown below:Then, display
name,ageandextra_infoinresult.htmlas shown below:Then, this below is displayed:
Actually, you can still pass
nameandagefromindex.htmltoperson_tag()as shown below:But,
person_tag()needs 2 more parameters as shown below. *Other names are fine fornameandageparameters:Lastly, return an empty dictionary from
person_tag()toresult.htmlas shown below:Then, display
name,ageandextra_infoinindex.htmlas shown below:Then, this below is displayed: